Commits

Jacek Bzdak committed 3600d1c

Version change

  • Participants
  • Parent commits 7a2a353

Comments (0)

Files changed (12)

commons-ejb/pom.xml

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

commons/src/main/java/cx/ath/jbzdak/common/ProcessUtils.java

 
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
 
-   public static void waitForProcessEnd(Process process, boolean redirectIO) throws IOException {
+   public static void waitForProcessEnd(final Process process, final boolean redirectIO) throws IOException {
       Lock lock = new ReentrantLock();
       final Condition condition = lock.newCondition();
       while (true){
             public void exec() {
                try {
                   condition.await(500, TimeUnit.MILLISECONDS);
+                  if(redirectIO){
+                     copy(process.getErrorStream(), System.err);
+                     copy(process.getInputStream(), System.err);
+                     System.out.flush();
+                  }else{
+                     discard(process.getInputStream());
+                     discard(process.getErrorStream());
+                  }
                } catch (InterruptedException e) {
                   return;
+               } catch (IOException e){
+                  throw new RuntimeException(e);
                }
             }
          });
                if(redirectIO){
                   copy(process.getErrorStream(), System.err);
                   copy(process.getInputStream(), System.out);
+                  System.out.flush();
                }else{
                   discard(process.getInputStream());
                   discard(process.getErrorStream());

commons/src/main/java/cx/ath/jbzdak/common/PropertyHolder.java

-package cx.ath.jbzdak.common;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.util.Properties;
-
-/**
- * Jacek Bzdak
- */
-public class PropertyHolder {
-
-   protected Properties properties;
-
-   public PropertyHolder(Class clazz, String defaultResources, String userResources) throws IOException {
-      properties = new Properties();
-      try {
-         properties.load(clazz.getResourceAsStream(defaultResources));
-      } catch (IOException e) {
-         throw new IOException("Couldnt load default properties from '" + defaultResources + "'", e);
-      } catch (NullPointerException e){
-         throw new IOException("Couldnt load default properties from '" + defaultResources + "'", e);
-      }
-      File file = new File(userResources);
-      file.getParentFile().mkdirs();
-      boolean exists = file.exists();
-      if (!exists){
-         file.createNewFile();
-      }
-      if (exists){
-//         properties = new Properties(properties);
-         properties.load(new FileInputStream(file));
-      }else{
-         properties.store(new FileOutputStream(file), "Auto generated");
-      }
-   }
-
-   public Properties getProperties() {
-      return properties;
-   }
-}

commons/src/main/java/cx/ath/jbzdak/common/fakeEnum/FakeEnum.java

             Modifier.isStatic(mod) &&
             Modifier.isFinal(mod) &&
             Modifier.isPublic(mod) &&
-            clazz.isAssignableFrom(clazz)){
+            clazz.isAssignableFrom(field.getType()) &&
+            field.getAnnotation(FakeEnumIgnore.class) == null){
             fields.add(field);
          }
       }
       return constants.get(s);
    }
 
+
+   public String genToString(E o) {
+      return o.getClass().getCanonicalName() + "@" + nameOf(o);
+   }
+
    public boolean equals(Object e1, Object e2){
       if (! constantsReversed.containsKey(e1) || ! constantsReversed.containsKey(e2)){
          return false;

commons/src/main/java/cx/ath/jbzdak/common/fakeEnum/FakeEnumIgnore.java

+package cx.ath.jbzdak.common.fakeEnum;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface FakeEnumIgnore {
+}

commons/src/main/java/cx/ath/jbzdak/common/fakeEnum/FakeEnumItem.java

 
    FakeEnum<FE> fakeEnum;
 
+   public FakeEnumItem(FakeEnum<FE> fakeEnum) {
+      this.fakeEnum = fakeEnum;
+   }
+
    public final String name(){
       return fakeEnum.nameOf((FE) this);
    }

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

+package cx.ath.jbzdak.common.nativeUtils;
+
+import cx.ath.jbzdak.common.fakeEnum.FakeEnum;
+
+import java.security.InvalidParameterException;
+import java.util.*;
+
+/**
+ * Class for enums that can be and'ed or or'ed to int constant
+ *
+ * Created by: Jacek Bzdak
+ */
+public abstract class MaskedEnum {
+
+   protected final int constant;
+
+   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;
+   }
+
+   protected MaskedEnum(int constant, boolean useInDecomposition) {
+      this.constant = constant;
+      this.useInDecomposition = useInDecomposition;
+   }
+
+   protected MaskedEnum(MaskedEnum maskedEnum) {
+      this.constant = maskedEnum.constant;
+      this.useInDecomposition = false;
+   }
+
+   public static int or(int base, MaskedEnum... enums){
+      int result = base;
+      for (MaskedEnum me : enums){
+         result |= me.constant;
+      }
+      return result;
+   }
+
+   public static int and(int base, MaskedEnum... enums){
+      int result = base;
+      for (MaskedEnum me : enums){
+         result &= me.constant;
+      }
+      return result;
+   }
+
+   public static int switchOn(int base, MaskedEnum... enums){
+      return or(base, enums);
+   }
+
+   public static int switchOff(int base, MaskedEnum... enums){
+      int result = base;
+      for (MaskedEnum me : enums){
+         result &= ~me.constant;
+      }
+      return result;
+   }
+
+   public static int or(MaskedEnum... enums){
+      return or(0, enums);
+   }
+
+   public static int switchOn(MaskedEnum... enums){
+      return or(0, enums);
+   }
+
+   public static int or(int base, FakeEnum<MaskedEnum> fakeEnum, String... enums){
+      int result = base;
+      for (String me : enums){
+         result |= fakeEnum.valueOf(me).constant;
+      }
+      return result;
+   }
+
+   public static int or(FakeEnum<MaskedEnum> fakeEnum, String... enums){
+      return or(0, fakeEnum, enums);
+   }
+
+   public static int switchOn(int base, FakeEnum<MaskedEnum> fakeEnum, String... enums){
+      return or(base, fakeEnum, enums);
+   }
+
+   public static int switchOn(FakeEnum<MaskedEnum> fakeEnum, String... enums){
+      return switchOn(0, fakeEnum, enums);
+   }
+
+   public static int and(int base, FakeEnum<MaskedEnum> fakeEnum, String... enums){
+      int result = base;
+      for (String me : enums){
+         result &= fakeEnum.valueOf(me).constant;
+      }
+      return result;
+   }
+
+   public static int switchOff(int base, FakeEnum<MaskedEnum> fakeEnum, String... enums){
+      int result = base;
+      for (String me : enums){
+         result &= fakeEnum.valueOf(me).constant;
+      }
+      return result;
+   }
+
+   public static int or(int base, FakeEnum<? extends MaskedEnum> fakeEnum, Iterable<String> enums){
+      int result = base;
+      for (String me : enums){
+         result |= fakeEnum.valueOf(me).constant;
+      }
+      return result;
+   }
+
+   public static int or(FakeEnum<? extends MaskedEnum> fakeEnum, Iterable<String> enums){
+      return or(0, fakeEnum, enums);
+   }
+
+   public static int switchOn(int base, FakeEnum<? extends MaskedEnum> fakeEnum, Iterable<String> enums){
+      return or(base, fakeEnum, enums);
+   }
+
+
+   public static int switchOn(FakeEnum<? extends MaskedEnum> fakeEnum, Iterable<String>  enums){
+      return switchOn(0, fakeEnum, enums);
+   }
+
+
+
+}

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

       try {
          properties.load(clazz.getResourceAsStream(defaultResources));
       } catch (IOException e) {
-         throw new IOException("Couldnt load default properties", e);
+         throw new IOException("Couldnt load default properties from '" + defaultResources + "'", e);
       } catch (NullPointerException e){
-         throw new IOException("Couldnt load default properties", e);
+         throw new IOException("Couldnt load default properties '" + defaultResources + "'", e);
       }
       File file = new File(userResources);
       file.getParentFile().mkdirs();

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

+package cx.ath.jbzdak.common.nativeUtils;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public class MaskedEnumTests {
+
+
+   @Before
+   public void setUp() throws Exception {
+
+   }
+
+   @Test
+   public void testTestBuild() throws Exception {
+      for (TestMaskedEnum anEnum : TestMaskedEnum.FAKE_ENUM.values()) {
+         Assert.assertEquals(anEnum.constant, TestMaskedEnum.or(anEnum));
+      }
+   }
+
+   @Test
+   public void testTestOr() throws Exception {
+      Assert.assertEquals(3, TestMaskedEnum.or(TestMaskedEnum.A0, TestMaskedEnum.A1));
+   }
+
+   @Test
+   public void testTestOrBase() throws Exception {
+      Assert.assertEquals(6, TestMaskedEnum.or(4, TestMaskedEnum.A1));
+   }
+
+   @Test
+   public void testTestAndNegative() throws Exception {
+      Assert.assertEquals(0, TestMaskedEnum.and(4, TestMaskedEnum.A1));
+   }
+
+   @Test
+   public void testAndPositive() throws Exception {
+      Assert.assertEquals(2, TestMaskedEnum.and(6, TestMaskedEnum.A1));
+   }
+
+   @Test
+   public void testSwitchUp() throws Exception {
+      Assert.assertEquals(2, TestMaskedEnum.switchOn(TestMaskedEnum.A1));
+   }
+
+   @Test
+   public void testSwitchUp2() throws Exception {
+      Assert.assertEquals(2, TestMaskedEnum.switchOn(2, TestMaskedEnum.A1));
+   }
+
+   @Test
+   public void testSwitchUp3() throws Exception {
+      Assert.assertEquals(6, TestMaskedEnum.switchOn(2, TestMaskedEnum.A2));
+   }
+
+
+   @Test
+   public void testDecompose() throws Exception {
+      Assert.assertEquals(new HashSet(Arrays.asList(TestMaskedEnum.A1, TestMaskedEnum.A2)), TestMaskedEnum.decomposite(TestMaskedEnum.MAP, 6));
+   }
+
+   @Test
+   public void testDecompose2() throws Exception {
+      Assert.assertEquals(new HashSet(Arrays.asList(TestMaskedEnum.A1, TestMaskedEnum.A0, TestMaskedEnum.MASK)), TestMaskedEnum.decomposite(TestMaskedEnum.MAP, 3));
+   }
+
+
+
+}
+

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

+package cx.ath.jbzdak.common.nativeUtils;
+
+import cx.ath.jbzdak.common.fakeEnum.FakeEnum;
+import junit.framework.Test;
+
+import java.util.Map;
+import java.util.NavigableMap;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public class TestMaskedEnum extends MaskedEnum{
+
+   public static final TestMaskedEnum A0 = new TestMaskedEnum(1);
+   public static final TestMaskedEnum A1 = new TestMaskedEnum(2);
+   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);
+
+   @Override
+   public String toString() {
+      return FAKE_ENUM.genToString(this);
+   }
+
+   public TestMaskedEnum(int constant) {
+      super(constant);
+   }
+
+   public TestMaskedEnum(int constant, boolean useInDecomposition) {
+      super(constant, useInDecomposition);
+   }
+
+   public TestMaskedEnum(MaskedEnum maskedEnum) {
+      super(maskedEnum);
+   }
+}
     <groupId>cx.ath.jbzdak.commons</groupId>
     <artifactId>commons-main</artifactId>
     <packaging>pom</packaging>
-    <version>2.0</version>
+    <version>2.0.2</version>
     <modules>
         <module>commons</module>
         <module>commons-ejb</module>
 
     <name>Miscalenneus libraries</name>
 
+
     <distributionManagement>
         <repository>
-            <name>Jacek Bzdak repo</name>
-            <id>archiva-skimble</id>
-            <url>https://skimbleshanks.ath.cx/archiva/repository/public</url>
+            <id>lfitj-ilf</id>
+            <name>lfitj releases</name>
+            <url>http://lfitj.if.pw.edu.pl/artifactory/shared</url>
         </repository>
+        <snapshotRepository>
+            <id>lfitj-ilf/</id>
+            <name>Lfitj snapshot</name>
+            <url>http://lfitj.if.pw.edu.pl/artifactory/shared</url>
+        </snapshotRepository>
     </distributionManagement>
 
     <repositories>
         <repository>
-            <id>skimble-public</id>
-            <url>http://skimbleshanks.ath.cx/archiva/repository/public-group/</url>
-            <!-- Prywatne repozytorium autora -->
+            <name>Repozytorium na lfitj</name>
+            <id>lfitj-ilf-group</id>
+            <url>http://lfitj.if.pw.edu.pl/artifactory/shared/</url>
         </repository>
     </repositories>
 
     </dependencies>
 
     <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-source-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>attach-sources</id>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
         <pluginManagement>
             <plugins>
                 <plugin>