[Japi-cvs] SF.net SVN: japi: [538] libs/lang/trunk/src
Status: Beta
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-07-14 10:40:41
|
Revision: 538
http://svn.sourceforge.net/japi/?rev=538&view=rev
Author: christianhujer
Date: 2007-07-14 03:40:40 -0700 (Sat, 14 Jul 2007)
Log Message:
-----------
Added property comparator.
Added Paths:
-----------
libs/lang/trunk/src/net/sf/japi/lang/PropertyComparator.java
libs/lang/trunk/src/net/sf/japi/lang/package-info.java
libs/lang/trunk/src/test/net/sf/japi/lang/PropertyComparatorTest.java
libs/lang/trunk/src/test/net/sf/japi/lang/package-info.java
Added: libs/lang/trunk/src/net/sf/japi/lang/PropertyComparator.java
===================================================================
--- libs/lang/trunk/src/net/sf/japi/lang/PropertyComparator.java (rev 0)
+++ libs/lang/trunk/src/net/sf/japi/lang/PropertyComparator.java 2007-07-14 10:40:40 UTC (rev 538)
@@ -0,0 +1,134 @@
+/*
+ * JAPI libs-lang is a library that contains some useful classes regarding java.lang.
+ * Copyright (C) 2007 Christian Hujer.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package net.sf.japi.lang;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Comparator;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * A Comparator for properties, compares two objects based on a Java Beans Property.
+ *
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ * @todo think whether net.sf.japi.lang really is the correct package or this should rather go to something like net.sf.japi.beans or net.sf.japi.util.
+ */
+public class PropertyComparator<T, C> implements Comparator<C> {
+
+ /** The target class. */
+ @Nullable private final Class<C> targetClass;
+
+ /** The comparator to compare the property value. */
+ @Nullable private final Comparator<T> delegate;
+
+ /** The name of the property to get. */
+ private final String propertyName;
+
+ /** The getter to read the property. */
+ @Nullable private final Method getter;
+
+ /** Create a PropertyComparator.
+ * @param targetClass The target class of which properties should be compared, maybe <code>null</code> in which case the target class will be evaluated dynamically.
+ * @param propertyName Name of the property to compare.
+ * @param delegate Delegate comparator for the property, maybe <code>null</code> in which natural ordering of the property will be assumed; the property type must implement {@link Comparable} then.
+ */
+ public PropertyComparator(@Nullable final Class<C> targetClass, @NotNull final String propertyName, @Nullable final Comparator<T> delegate) {
+ this.targetClass = targetClass;
+ this.propertyName = propertyName;
+ this.delegate = delegate;
+ getter = targetClass == null ? null : getPropertyGetter(targetClass, propertyName);
+ }
+
+ /** {@inheritDoc} */
+ public int compare(final C o1, final C o2) {
+ try {
+ final Method o1Getter = getter != null ? getter : getPropertyGetter(o1.getClass(), propertyName);
+ final Method o2Getter = getter != null ? getter : getPropertyGetter(o2.getClass(), propertyName);
+ final Object o1Value = o1Getter.invoke(o1);
+ final Object o2Value = o2Getter.invoke(o2);
+ if (delegate == null) {
+ return ((Comparable) o1Value).compareTo((Comparable) o2Value);
+ } else {
+ return delegate.compare((T) o1Value, (T) o2Value);
+ }
+ } catch (final IllegalAccessException e) {
+ throw new IllegalAccessError(e.getMessage());
+ } catch (final InvocationTargetException e) {
+ throw new RuntimeException(e); // TODO use something better than RuntimeException
+ }
+ }
+
+ /** Returns the property getter for the specified class and property name.
+ * @param targetClass Class to get property getter for.
+ * @param propertyName Name of the property to get getter for.
+ * @return Getter Method for the specified property.
+ */
+ public static Method getPropertyGetter(@NotNull final Class targetClass, @NotNull final String propertyName) {
+ try { // try getFoo()
+ final StringBuilder getterName = new StringBuilder();
+ getterName.append("get");
+ getterName.append(propertyName);
+ getterName.setCharAt(3, Character.toUpperCase(getterName.charAt(3)));
+ return targetClass.getMethod(getterName.toString());
+ } catch (final NoSuchMethodException ignore) {
+ // one more try: boolean property name.
+ }
+ try { // try isFoo()
+ final StringBuilder getterName = new StringBuilder();
+ getterName.append("is");
+ getterName.append(propertyName);
+ getterName.setCharAt(2, Character.toUpperCase(getterName.charAt(2)));
+ return targetClass.getMethod(getterName.toString());
+ } catch (final NoSuchMethodException ignore) {
+ // one more try: property name.
+ }
+ try { // try foo()
+ return targetClass.getMethod(propertyName);
+ } catch (final NoSuchMethodException ignore) {
+ // one more try: property name.
+ }
+ throw new NoSuchMethodError("Property Getter for property " + propertyName + " in class " + targetClass);
+ }
+
+ /** Returns the property name of the property that's compared by this PropertyComparator.
+ * @return Property name.
+ */
+ public String getPropertyName() {
+ return propertyName;
+ }
+
+ /** Returns the target class this PropertyComparator operates on.
+ * @return Target class or <code>null</code> if this PropertyComparator uses dynamic class evaluation.
+ */
+ @Nullable
+ public Class<C> getTargetClass() {
+ return targetClass;
+ }
+
+ /** Returns the delegate this PropertyComparator uses for the property value comparison.
+ * @return Delegate or <code>null</code> if this PropertyComparator uses natural ordering.
+ */
+ @Nullable
+ public Comparator<T> getDelegate() {
+ return delegate;
+ }
+
+} // class PropertyComparator
Property changes on: libs/lang/trunk/src/net/sf/japi/lang/PropertyComparator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: libs/lang/trunk/src/net/sf/japi/lang/package-info.java
===================================================================
--- libs/lang/trunk/src/net/sf/japi/lang/package-info.java (rev 0)
+++ libs/lang/trunk/src/net/sf/japi/lang/package-info.java 2007-07-14 10:40:40 UTC (rev 538)
@@ -0,0 +1,20 @@
+/*
+ * JAPI libs-lang is a library that contains some useful classes regarding java.lang.
+ * Copyright (C) 2007 Christian Hujer.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package net.sf.japi.lang;
Property changes on: libs/lang/trunk/src/net/sf/japi/lang/package-info.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: libs/lang/trunk/src/test/net/sf/japi/lang/PropertyComparatorTest.java
===================================================================
--- libs/lang/trunk/src/test/net/sf/japi/lang/PropertyComparatorTest.java (rev 0)
+++ libs/lang/trunk/src/test/net/sf/japi/lang/PropertyComparatorTest.java 2007-07-14 10:40:40 UTC (rev 538)
@@ -0,0 +1,47 @@
+/*
+ * JAPI libs-lang is a library that contains some useful classes regarding java.lang.
+ * Copyright (C) 2007 Christian Hujer.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package test.net.sf.japi.lang;
+
+import net.sf.japi.lang.PropertyComparator;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test for {@link PropertyComparator}.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class PropertyComparatorTest {
+
+ /** Test the use case comparing String lengths. */
+ @Test
+ public void testStringLengthExample() {
+ final String s1 = "hello";
+ final String s2 = "world";
+ final String s3 = "a";
+ final String s4 = "nice";
+ final String s5 = "day";
+ final PropertyComparator<Integer, String> lengthComparator = new PropertyComparator<Integer, String>(String.class, "length", null);
+ Assert.assertTrue(lengthComparator.compare(s1, s2) == 0);
+ Assert.assertTrue(lengthComparator.compare(s2, s3) > 0);
+ Assert.assertTrue(lengthComparator.compare(s3, s4) < 0);
+ Assert.assertTrue(lengthComparator.compare(s4, s5) > 0);
+ }
+
+} // class PropertyComparatorTest
Property changes on: libs/lang/trunk/src/test/net/sf/japi/lang/PropertyComparatorTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
Added: libs/lang/trunk/src/test/net/sf/japi/lang/package-info.java
===================================================================
--- libs/lang/trunk/src/test/net/sf/japi/lang/package-info.java (rev 0)
+++ libs/lang/trunk/src/test/net/sf/japi/lang/package-info.java 2007-07-14 10:40:40 UTC (rev 538)
@@ -0,0 +1,20 @@
+/*
+ * JAPI libs-lang is a library that contains some useful classes regarding java.lang.
+ * Copyright (C) 2007 Christian Hujer.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package test.net.sf.japi.lang;
Property changes on: libs/lang/trunk/src/test/net/sf/japi/lang/package-info.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|