Commits

Jacek Bzdak committed 5956164

Added utilities for enums that represents flags.

Comments (0)

Files changed (11)

commons-ejb/pom.xml

     <parent>
         <artifactId>commons-main</artifactId>
         <groupId>cx.ath.jbzdak.commons</groupId>
-        <version>2.0.2</version>
+        <version>2.0.3</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 
     <parent>
         <artifactId>commons-main</artifactId>
         <groupId>cx.ath.jbzdak.commons</groupId>
-        <version>2.0.2</version>
+        <version>2.0.3</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 

commons/src/main/java/cx/ath/jbzdak/common/annotation/Default.java

+package cx.ath.jbzdak.common.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * This annotation means that in enum that represents some possible settings annotated setting is the default.
+ * Created by: Jacek Bzdak
+ */
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Default { }

commons/src/main/java/cx/ath/jbzdak/common/nativeUtils/MaskedEnum.java

 
    protected final boolean useInDecomposition;
 
-   protected static <T extends MaskedEnum> NavigableMap<Integer, T> collect(FakeEnum<T> fakeEnum){
-      NavigableMap<Integer, T> result = new TreeMap<Integer, T>();
-      for (T t : fakeEnum.values()) {
-         if(!t.useInDecomposition){
-            continue;
-         }
-         if(result.containsKey(t.constant)){
-            throw new InvalidParameterException("Constants in navigable map cant repeat!");
-         }
-         result.put(t.constant, t);
-      }
-      return result;
-   }
-
-   protected static <T extends MaskedEnum> Set<T> decomposite(NavigableMap<Integer, T> map, int value){
-      Set<T> tSet = new HashSet<T>();
-      for (Map.Entry<Integer, T> entry : map.entrySet()) {
-         Integer key = entry.getKey();
-         if((value & key) == key){
-            tSet.add(entry.getValue());
-         }
-      }
-      return tSet;
-   }
-
    protected MaskedEnum(int constant) {
       this.constant = constant;
       this.useInDecomposition = true;
       this.useInDecomposition = false;
    }
 
+   public int getConstant() {
+      return constant;
+   }
+
+
    public static int or(int base, MaskedEnum... enums){
       int result = base;
       for (MaskedEnum me : enums){
       return result;
    }
 
+   public static int and(MaskedEnum... enums){
+       return and(0, enums);
+    }
+
+
    public static int switchOn(int base, MaskedEnum... enums){
       return or(base, enums);
    }

commons/src/main/java/cx/ath/jbzdak/common/nativeUtils/MaskedFakeEnum.java

+package cx.ath.jbzdak.common.nativeUtils;
+
+import cx.ath.jbzdak.common.fakeEnum.FakeEnum;
+
+import java.security.InvalidParameterException;
+import java.util.*;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public class MaskedFakeEnum<T extends MaskedEnum> extends FakeEnum<T>{
+
+//   private final NavigableMap<Integer, T> collectedMap;
+
+
+   public Set<T> decomposite(int value){
+      Set<T> tSet = new HashSet<T>();
+      for (T t : values()) {
+         Integer key = t.constant;
+         if((value & key) == key){
+            tSet.add(t);
+         }
+      }
+      return tSet;
+   }
+
+   public MaskedFakeEnum(Class<T> clazz) {
+      super(clazz);
+   }
+
+
+
+
+}

commons/src/main/java/cx/ath/jbzdak/common/properties/AbstractExtendedProperties.java

       defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    }
 
+   public AbstractExtendedProperties createWrapped(String prefix){
+      return new WrappedProperties(this, true, prefix);
+   }
+
    public <T> T getValue(String keyName, Transformer<T> transformer, T defaultValue){
       String value = getPropertyInternal(keyName);
       if (value == null){

commons/src/main/java/cx/ath/jbzdak/common/properties/MapProperties.java

+package cx.ath.jbzdak.common.properties;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public class MapProperties extends AbstractExtendedProperties{
+
+
+   private final Map<String, String> internal;
+
+   public MapProperties(Map<String, String> internal) {
+      this.internal = internal;
+   }
+
+   @Override
+   protected String getPropertyInternal(String keyName) {
+      return internal.get(keyName);
+   }
+
+   @Override
+   protected String setPropertyInternal(String keyName, String value) {
+      return internal.put(keyName, value);
+   }
+
+   @Override
+   public Map<String, String> getAsMap() {
+      return internal;
+   }
+}

commons/src/main/java/cx/ath/jbzdak/common/properties/WrappedProperties.java

+package cx.ath.jbzdak.common.properties;
+
+import java.io.InvalidObjectException;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public class WrappedProperties  extends AbstractExtendedProperties{
+
+   final AbstractExtendedProperties parent;
+
+   final boolean readOnly;
+
+   final String prefix;
+
+   Map<String, String> prefixedMap;
+
+   public WrappedProperties(AbstractExtendedProperties parent, boolean readOnly, String prefix) {
+      this.parent = parent;
+      this.readOnly = readOnly;
+      if(! prefix.endsWith(".")){
+         prefix = prefix + ".";
+      }
+      this.prefix = prefix;
+   }
+
+   public WrappedProperties(AbstractExtendedProperties parent, String prefix) {
+      this(parent, true, prefix);
+   }
+
+   @Override
+   protected String getPropertyInternal(String keyName) {
+      return parent.getString(prefix + keyName);
+   }
+
+   @Override
+   protected String setPropertyInternal(String keyName, String value) {
+      if(readOnly){
+         throw new UnsupportedOperationException("Can't change immutable Properties");
+      }
+
+      return parent.setPropertyInternal(prefix + keyName, value);
+   }
+
+   @Override
+   public Map<String, String> getAsMap() {
+      if (prefixedMap == null){
+         synchronized (this){
+            if(prefixedMap == null){
+               Map<String, String> prefixedMap = new ConcurrentHashMap<String, String>();
+               for (Map.Entry<String, String> entry : parent.getAsMap().entrySet()) {
+                  String key = entry.getKey();
+                  int length = prefix.length();
+                  if(key.startsWith(prefix)){
+                     prefixedMap.put(key.substring(length), entry.getValue());
+                  }
+                  this.prefixedMap = prefixedMap;
+               }
+            }
+         }
+      }
+      return prefixedMap;
+   }
+}

commons/src/test/java/cx/ath/jbzdak/common/nativeUtils/MaskedEnumTests.java

 
    @Test
    public void testDecompose() throws Exception {
-      Assert.assertEquals(new HashSet(Arrays.asList(TestMaskedEnum.A1, TestMaskedEnum.A2)), TestMaskedEnum.decomposite(TestMaskedEnum.MAP, 6));
+      Assert.assertEquals(new HashSet(Arrays.asList(TestMaskedEnum.A1, TestMaskedEnum.A2)), TestMaskedEnum.FAKE_ENUM.decomposite(6));
    }
 
    @Test
    public void testDecompose2() throws Exception {
-      Assert.assertEquals(new HashSet(Arrays.asList(TestMaskedEnum.A1, TestMaskedEnum.A0, TestMaskedEnum.MASK)), TestMaskedEnum.decomposite(TestMaskedEnum.MAP, 3));
+      Assert.assertEquals(new HashSet(Arrays.asList(TestMaskedEnum.A1, TestMaskedEnum.A0, TestMaskedEnum.MASK)), TestMaskedEnum.FAKE_ENUM.decomposite(3));
    }
 
 

commons/src/test/java/cx/ath/jbzdak/common/nativeUtils/TestMaskedEnum.java

    public static final TestMaskedEnum A2 = new TestMaskedEnum(4);
    public static final TestMaskedEnum MASK = new TestMaskedEnum(3);
 
-   public static final FakeEnum<TestMaskedEnum> FAKE_ENUM = new FakeEnum<TestMaskedEnum>(TestMaskedEnum.class);
-
-   public static final NavigableMap<Integer, TestMaskedEnum> MAP = TestMaskedEnum.collect(FAKE_ENUM);
+   public static final MaskedFakeEnum<TestMaskedEnum> FAKE_ENUM = new MaskedFakeEnum<TestMaskedEnum>(TestMaskedEnum.class);
 
    @Override
    public String toString() {
     <groupId>cx.ath.jbzdak.commons</groupId>
     <artifactId>commons-main</artifactId>
     <packaging>pom</packaging>
-    <version>2.0.2</version>
+    <version>2.0.3</version>
     <modules>
         <module>commons</module>
         <module>commons-ejb</module>