From: Erik Brakkee Date: Wed, 21 Jul 2010 10:42:54 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.7~239 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=7c1f4aa47fcc3098427073b5ce9f6abed8befa7c;p=utils --- diff --git a/support/general/src/main/java/org/wamblee/persistence/Accessor.java b/support/general/src/main/java/org/wamblee/persistence/Accessor.java new file mode 100644 index 00000000..58b6cf7f --- /dev/null +++ b/support/general/src/main/java/org/wamblee/persistence/Accessor.java @@ -0,0 +1,40 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.persistence; + +/** + * Abstraction for accessing fields or properties of an object. + * + * @author Erik Brakkee + * + * @param + */ +public interface Accessor { + /** + * Sets the value. + * @param aObject Object. + * @param aValue Value. + */ + void set(Object aObject, T aValue); + + /** + * Gets the value. + * @param aObject Object + * @return Value + */ + T get(Object aObject); +} + diff --git a/support/general/src/main/java/org/wamblee/persistence/FieldAccessor.java b/support/general/src/main/java/org/wamblee/persistence/FieldAccessor.java new file mode 100644 index 00000000..e2de4eec --- /dev/null +++ b/support/general/src/main/java/org/wamblee/persistence/FieldAccessor.java @@ -0,0 +1,65 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.persistence; + +import java.lang.reflect.Field; + +/** + * Utility for interfacing to a field of an object. + * + * @author Erik Brakkee + * + * @param Type of the field. + */ +class FieldAccessor implements Accessor { + private Field field; + + /** + * Constructs the accessor. + * @param aField Field. + */ + public FieldAccessor(Field aField) { + field = aField; + field.setAccessible(true); + } + + @Override + public T get(Object aEntity) { + try { + T value = (T) field.get(aEntity); + return value; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void set(Object aEntity, T aValue) { + try { + field.set(aEntity, aValue); + } catch (IllegalAccessException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + /** + * Gets the field. + * @return Field. + */ + public Field getField() { + return field; + } +} \ No newline at end of file diff --git a/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java b/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java index 944bd34a..bd1217e6 100644 --- a/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java +++ b/support/general/src/main/java/org/wamblee/persistence/PersistentFactory.java @@ -46,82 +46,6 @@ public class PersistentFactory { */ private static Map CACHE = new ConcurrentHashMap(); - public static interface Accessor { - void set(Object aEntity, T aValue); - - T get(Object aEntity); - } - - static class FieldAccessor implements Accessor { - private Field field; - - public FieldAccessor(Field aField) { - field = aField; - field.setAccessible(true); - } - - @Override - public T get(Object aEntity) { - try { - T value = (T) field.get(aEntity); - return value; - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @Override - public void set(Object aEntity, T aValue) { - try { - field.set(aEntity, aValue); - } catch (IllegalAccessException e) { - throw new RuntimeException(e.getMessage(), e); - } - } - - public Field getField() { - return field; - } - } - - static class PropertyAccessor implements Accessor { - private Method getter; - private Method setter; - - public PropertyAccessor(Method aGetter, Method aSetter) { - getter = aGetter; - setter = aSetter; - getter.setAccessible(true); - setter.setAccessible(true); - } - - @Override - public T get(Object aEntity) { - try { - return (T) getter.invoke(aEntity); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @Override - public void set(Object aEntity, T aValue) { - try { - setter.invoke(aEntity, aValue); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - public Method getGetter() { - return getter; - } - - public Method getSetter() { - return setter; - } - } - static class EntityAccessor { private Accessor pk; private Accessor version; diff --git a/support/general/src/main/java/org/wamblee/persistence/PropertyAccessor.java b/support/general/src/main/java/org/wamblee/persistence/PropertyAccessor.java new file mode 100644 index 00000000..baf539b0 --- /dev/null +++ b/support/general/src/main/java/org/wamblee/persistence/PropertyAccessor.java @@ -0,0 +1,74 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.persistence; + +import java.lang.reflect.Method; + +/** + * Accessing a property of an object. + * + * @author Erik Brakkee + * + * @param Type of the property. + */ +class PropertyAccessor implements Accessor { + private Method getter; + private Method setter; + + /** + * Constructs the accessor. + * @param aGetter Getter method. + * @param aSetter Setter method. + */ + public PropertyAccessor(Method aGetter, Method aSetter) { + getter = aGetter; + setter = aSetter; + getter.setAccessible(true); + setter.setAccessible(true); + } + + @Override + public T get(Object aEntity) { + try { + return (T) getter.invoke(aEntity); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void set(Object aEntity, T aValue) { + try { + setter.invoke(aEntity, aValue); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * @return The getter. + */ + public Method getGetter() { + return getter; + } + + /** + * @return The setter. + */ + public Method getSetter() { + return setter; + } +} \ No newline at end of file diff --git a/support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java b/support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java new file mode 100644 index 00000000..5c701fb5 --- /dev/null +++ b/support/general/src/main/java/org/wamblee/reflection/AnnotationUtils.java @@ -0,0 +1,26 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.reflection; + +/** + * Utlities for working with annotations. + * + * @author Erik Brakkee + * + */ +public class AnnotationUtils { + +} diff --git a/support/general/src/test/java/org/wamblee/persistence/PersistentFactoryTest.java b/support/general/src/test/java/org/wamblee/persistence/PersistentFactoryTest.java index 16979de7..82f0beba 100644 --- a/support/general/src/test/java/org/wamblee/persistence/PersistentFactoryTest.java +++ b/support/general/src/test/java/org/wamblee/persistence/PersistentFactoryTest.java @@ -21,11 +21,8 @@ import javax.persistence.Id; import javax.persistence.Version; import org.junit.Test; -import org.wamblee.persistence.PersistentFactory.Accessor; import org.wamblee.persistence.PersistentFactory.EntityAccessor; import org.wamblee.persistence.PersistentFactory.EntityObjectAccessor; -import org.wamblee.persistence.PersistentFactory.FieldAccessor; -import org.wamblee.persistence.PersistentFactory.PropertyAccessor; public class PersistentFactoryTest {