Commits

Jacek Bzdak committed 482423c

Nowe ExtendedProperties

  • Participants
  • Parent commits 4a33082

Comments (0)

Files changed (12)

commons-ejb/src/main/java/cx/ath/jbzdak/common/db/DBUtils.java

  * Created by: Jacek Bzdak
  */
 public class DBUtils {
+
    public static <T> T executeInTransaction(EntityManagerFactory entityManagerFactory, ReturnableTransactionExecutor<T> executor) {
       EntityManager entityManager = entityManagerFactory.createEntityManager();
       T result;
          entityManager.getTransaction().begin();
          result = executor.execute(entityManager);
          entityManager.getTransaction().commit();
+         return result;
       } catch (RuntimeException e){
          if(entityManager.getTransaction().isActive()){
             entityManager.getTransaction().rollback();
          }
          throw e;
       } finally {
-         entityManager.close();;
+         entityManager.close();
       }
-      return result;
    }
 
 

commons/src/main/java/cx/ath/jbzdak/common/collections/TwoWayTransformer.java

+package cx.ath.jbzdak.common.collections;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public interface TwoWayTransformer<T, V> extends Transformer<T, V> {
+
+   V transformReverse(T value);
+}

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

+package cx.ath.jbzdak.common.properties;
+
+import com.sun.org.apache.xerces.internal.impl.dv.xs.IntegerDV;
+import com.sun.xml.internal.messaging.saaj.util.FinalArrayList;
+import cx.ath.jbzdak.common.properties.transformer.DateTransformer;
+import cx.ath.jbzdak.common.properties.transformer.IntTransformer;
+import cx.ath.jbzdak.common.properties.transformer.NoopTransformer;
+import cx.ath.jbzdak.common.properties.transformer.Transformer;
+
+import java.security.InvalidParameterException;
+import java.text.DateFormat;
+import java.text.NumberFormat;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public abstract class AbstractExtendedProperties extends AbstractMap<String, String>{
+
+   protected DateFormat defaultDateFormat;
+
+   protected abstract String getPropertyInternal(String keyName);
+
+   protected AbstractExtendedProperties() {
+      defaultDateFormat = new SimpleDateFormat("YYYY-MM-DD");
+   }
+
+   public <T> T getValue(String keyName, Transformer<T> transformer, T defaultValue){
+      String value = getPropertyInternal(keyName);
+      if (value == null){
+         return defaultValue;
+      }
+      return transformer.transformReverse(value);
+   }
+
+   public <T> T getValue(String keyName, Transformer<T> transformer){
+      return getValue(keyName, transformer, null);
+   }
+
+   public Date getDate(String keyName, DateFormat dateFormat, Date defaultDate){
+      return getValue(keyName, new DateTransformer(dateFormat), defaultDate);
+   }
+
+   public Date getDate(String keyName, Date defaultDate){
+      return getValue(keyName, new DateTransformer(defaultDateFormat), defaultDate);
+   }
+
+   public Date getDate(String keyName){
+      return getValue(keyName, new DateTransformer(defaultDateFormat));
+   }
+
+   public void setDefaultDateFormat(DateFormat defaultDateFormat) {
+      this.defaultDateFormat = defaultDateFormat;
+   }
+
+   public Integer getInt(String keyName, NumberFormat numberFormat, Integer defaultInt){
+      return getValue(keyName, new IntTransformer(numberFormat), defaultInt);
+   }
+
+   public Integer getInt(String keyName, Integer defaultInt){
+      return getValue(keyName, new IntTransformer(), defaultInt);
+   }
+
+   public Integer getInt(String keyName){
+      return getValue(keyName, new IntTransformer(), null);
+   }
+
+   public <T> Collection<T> getMatching(String pattern, Transformer<T> transformer){
+      Pattern p = Pattern.compile(pattern);
+      List<T> result = new ArrayList<T>();
+      for (Map.Entry<String, String> e : entrySet()) {
+         if(p.matcher(e.getKey()).matches()){
+            result.add(transformer.transformReverse(e.getValue()));
+         }
+      }
+      return result;
+   }
+
+   public Collection<String> getMatching(String pattern){
+      return getMatching(pattern, NoopTransformer.NOOP_TRANSFORMER);
+   }
+
+
+   public <K, V> Set<Entry<K, V>> getMatchingEntries(String pattern,
+                                                     cx.ath.jbzdak.common.collections.Transformer<K, String> keyTransformer,
+                                                     cx.ath.jbzdak.common.collections.Transformer<V, String> valueTransformer){
+      Pattern p = Pattern.compile(pattern);
+      Set<Entry<K, V>> result = new HashSet<Entry<K, V>>();
+      for (Entry<String, String> e : entrySet()) {
+         Matcher matcher = p.matcher(e.getKey());
+         if(matcher.matches()){
+            switch (matcher.groupCount()){
+               case 0:
+                  result.add(new SimpleImmutableEntry<K, V>(keyTransformer.transform(matcher.group()),
+                       valueTransformer.transform(e.getValue())));
+                  break;
+               case 1:
+                  result.add(new SimpleImmutableEntry<K, V>(keyTransformer.transform(matcher.group(1)),
+                       valueTransformer.transform(e.getValue())));
+                  break;
+               default:
+                  throw new InvalidParameterException();
+
+            }
+         }
+      }
+      return result;
+   }
+
+
+   private static void assertLen(List<?> objects, int len){
+      if(objects.size() <= len){
+         while(objects.size() <= len){
+            objects.add(null);
+         }
+      }
+   }
+
+    /**
+    * Returns list of values.
+    * @param keyPattern pattern of keys to be added to list. Must contain single group that matches digits. For example
+    *    <code>enty.(\\d+)</code> contents of that group will be used index of value in the list.
+    * @return
+    */
+   public <T> List<T> getList(String keyPattern, cx.ath.jbzdak.common.collections.Transformer<T, String> transformer){
+      Pattern pattern = Pattern.compile(keyPattern);
+      NavigableMap<Integer, T> result = new TreeMap<Integer, T>();
+      for (Entry<String, String> entry : entrySet()) {
+         final Matcher matcher = pattern.matcher(entry.getKey());
+         if(matcher.matches()){
+            int index = Integer.parseInt(matcher.group(1));
+            result.put(index, transformer.transform(entry.getValue()));
+         }
+      }
+      return new ArrayList<T>(result.values());
+   }
+
+   public List<String> getList(String keyPattern){
+      return getList(keyPattern, NoopTransformer.NOOP_TRANSFORMER);
+   }
+
+
+
+
+
+}

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

 /**
  * Jacek Bzdak
  */
-public class ExtendedProperties extends AbstractMap<String, String> implements Map<String, String> {
+public class ExtendedProperties extends AbstractExtendedProperties {
 
    Properties properties;
 
       return (String) properties.get(key);
    }
 
-   public int getAsInt(String key){
-      return Integer.parseInt(get(key));
-   }
-
-   public Date getAsDate(String key, DateFormat dateFormat) throws ParseException {
-      return dateFormat.parse(get(key));
-   }
-
-   public Date getAsDate(String key, String dateFormat) throws ParseException {
-      return new SimpleDateFormat(dateFormat).parse(get(key));
-   }
-
-   public Date getAsDate(String key) throws ParseException {
-      return defaultDateFormat.parse(get(key));
-   }
-
-   private static void assertLen(List<?> objects, int len){
-      if(objects.size() <= len){
-         while(objects.size() <= len){
-            objects.add(null);
-         }
-      }
-   }
-
-   public Collection<String> getMatching(String pattern){
-      Pattern p = Pattern.compile(pattern);
-      List<String> result = new ArrayList<String>();
-      for (Entry<String, String> e : entrySet()) {
-         if(p.matcher(e.getKey()).matches()){
-            result.add(e.getValue());
-         }
-      }
-      return result;
-   }
-
-   /**
-    * Returns set that consists of pairs <code>(pattern.mather(key).group(1), value)</code>
-    * @param pattern
-    * @return
-    */
-   public Set<Entry<String, String>> getMatchingEntries(String pattern){
-      Pattern p = Pattern.compile(pattern);
-      Set<Entry<String, String>> result = new HashSet<Entry<String, String>>();
-      for (Entry<String, String> e : entrySet()) {
-         Matcher matcher = p.matcher(e.getKey());
-         if(matcher.matches()){
-            result.add(new SimpleImmutableEntry<String, String>(matcher.group(1), e.getValue()));
-         }
-      }
-      return result;
-   }
-
-   /**
-    * Returns set that consists of pairs <code>(pattern.mather(key).group(1), value)</code>
-    * @param pattern
-    * @return
-    */
-   public <K, V> Set<Entry<K, V>> getMatchingEntries(String pattern,
-                                                        Transformer<K, String> keyTransformer,
-                                                        Transformer<V, String> valueTransformer){
-      Pattern p = Pattern.compile(pattern);
-      Set<Entry<K, V>> result = new HashSet<Entry<K, V>>();
-      for (Entry<String, String> e : entrySet()) {
-         Matcher matcher = p.matcher(e.getKey());
-         if(matcher.matches()){
-            result.add(new SimpleImmutableEntry<K, V>(keyTransformer.transform(matcher.group(1)),
-                                                      valueTransformer.transform(e.getValue())));
-         }
-      }
-      return result;
-   }
-
-
-   /**
-    * Returns list of values.
-    * @param keyPattern pattern of keys to be added to list. Must contain single group that matches digits. For example
-    *    <code>enty.(\\d+)</code> contents of that group will be used index of value in the list.
-    * @return
-    */
-   public List<String> getList(String keyPattern){
-      Pattern pattern = Pattern.compile(keyPattern);
-      List<String> result = new ArrayList<String>();
-      for (Entry<String, String> entry : entrySet()) {
-         final Matcher matcher = pattern.matcher(entry.getKey());
-         if(matcher.matches()){
-            int index = Integer.parseInt(matcher.group(1));
-            assertLen(result, index);
-            result.set(index, entry.getValue());
-         }
-      }
-      while (result.remove(null));
-      return result;
-   }
-
-   public <T> List<T> getList(String keyPattern, Transformer<T, String> transformer){
-      List<String> strings = getList(keyPattern);
-      List<T> result = new ArrayList<T>(strings.size());
-      for (String string : strings) {
-         result.add(transformer.transform(string));
-      }
-      return result;
+   @Override
+   protected String getPropertyInternal(String keyName) {
+      return properties.getProperty(keyName);
    }
 
-
-
-//   public int get
-
    @Override
    public Set<Entry<String, String>> entrySet() {
       return new AbstractSet<Entry<String, String>>() {
       };
    }
 
-
 }
 
 

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

-package cx.ath.jbzdak.common.properties;
-
-import cx.ath.jbzdak.common.collections.Transformer;
-import cx.ath.jbzdak.common.fakeEnum.FakeEnum;
-
-/**
- * @author Jacek Bzdak
- */
-public class FakeEnumTransformer<T> implements Transformer<T,String>{
-
-   private final FakeEnum<T> fakeEnum;
-
-   public FakeEnumTransformer(FakeEnum<T> fakeEnum) {
-      this.fakeEnum = fakeEnum;
-   }
-
-   public T transform(String value) {
-      return fakeEnum.valueOf(value);
-   }
-}
-

commons/src/main/java/cx/ath/jbzdak/common/properties/transformer/DateTransformer.java

+package cx.ath.jbzdak.common.properties.transformer;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public class DateTransformer implements Transformer<Date>{
+
+   private final DateFormat dateFormat;
+
+   public DateTransformer() {
+      dateFormat = new SimpleDateFormat("YYYY-MM-DD");
+   }
+
+   public DateTransformer(DateFormat dateFormat) {
+      this.dateFormat = dateFormat;
+   }
+
+
+   public Date transformReverse(String from) {
+      try {
+         return dateFormat.parse(from);
+      } catch (ParseException e) {
+         throw new TransformationException(e);
+      }
+   }
+
+   public String transform(Date value) {
+       return dateFormat.format(value);
+   }
+}

commons/src/main/java/cx/ath/jbzdak/common/properties/transformer/EnumTransformer.java

+package cx.ath.jbzdak.common.properties.transformer;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public class EnumTransformer<E extends Enum<E>> implements Transformer<E>{
+
+   Class<E> clazz;
+
+   public EnumTransformer(Class<E> clazz) {
+      this.clazz = clazz;
+   }
+
+   public E transformReverse(String value) {
+      return Enum.valueOf(clazz, value);
+   }
+
+   public String transform(E value) {
+      return value.name();
+   }
+}

commons/src/main/java/cx/ath/jbzdak/common/properties/transformer/FakeEnumTransformer.java

+package cx.ath.jbzdak.common.properties.transformer;
+
+import cx.ath.jbzdak.common.fakeEnum.FakeEnum;
+
+/**
+ * @author Jacek Bzdak
+ */
+public class FakeEnumTransformer<T> implements Transformer<T>{
+
+   private final FakeEnum<T> fakeEnum;
+
+   public FakeEnumTransformer(FakeEnum<T> fakeEnum) {
+      this.fakeEnum = fakeEnum;
+   }
+
+   public T transformReverse(String value) {
+      return fakeEnum.valueOf(value);
+   }
+
+   public String transform(T value) {
+      return fakeEnum.nameOf(value);
+   }
+}
+

commons/src/main/java/cx/ath/jbzdak/common/properties/transformer/IntTransformer.java

+package cx.ath.jbzdak.common.properties.transformer;
+
+import net.jcip.annotations.NotThreadSafe;
+
+import java.text.NumberFormat;
+import java.text.ParseException;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+@NotThreadSafe
+public class IntTransformer implements Transformer<Integer>{
+
+   private final NumberFormat numberFormat;
+
+   public IntTransformer() {
+      numberFormat = NumberFormat.getIntegerInstance();
+   }
+
+   public IntTransformer(NumberFormat numberFormat) {
+      this.numberFormat = numberFormat;
+   }
+
+   public Integer transformReverse(String value) {
+      try {
+         return this.numberFormat.parse(value).intValue();
+      } catch (ParseException e) {
+         throw  new TransformationException("Parse exception", e);
+      }
+   }
+
+   public String transform(Integer value) {
+      return this.numberFormat.format(value);
+   }
+}

commons/src/main/java/cx/ath/jbzdak/common/properties/transformer/NoopTransformer.java

+package cx.ath.jbzdak.common.properties.transformer;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public class NoopTransformer implements Transformer<String> {
+
+   public static final NoopTransformer NOOP_TRANSFORMER = new NoopTransformer();
+
+   private NoopTransformer() {
+   }
+
+   public String transformReverse(String value) {
+      return value;
+   }
+
+   public String transform(String value) {
+      return value;
+   }
+}

commons/src/main/java/cx/ath/jbzdak/common/properties/transformer/TransformationException.java

+package cx.ath.jbzdak.common.properties.transformer;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public class TransformationException  extends RuntimeException{
+
+   public TransformationException(String message) {
+      super(message);
+   }
+
+   public TransformationException(String message, Throwable cause) {
+      super(message, cause);
+   }
+
+   public TransformationException(Throwable cause) {
+      super(cause);
+   }
+}

commons/src/main/java/cx/ath/jbzdak/common/properties/transformer/Transformer.java

+package cx.ath.jbzdak.common.properties.transformer;
+
+import cx.ath.jbzdak.common.collections.TwoWayTransformer;
+
+import javax.xml.transform.TransformerException;
+
+/**
+ * Created by: Jacek Bzdak
+ */
+public interface Transformer<V> extends TwoWayTransformer<String, V>{}