You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2010/01/23 06:39:30 UTC
svn commit: r902342 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base:
test/BaseUnitTests.java util/ComparableRange.java util/Range.java
Author: adrianc
Date: Sat Jan 23 05:39:30 2010
New Revision: 902342
URL: http://svn.apache.org/viewvc?rev=902342&view=rev
Log:
Created a Range interface, and an implementation for all Comparable classes.
Added:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ComparableRange.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Range.java (with props)
Modified:
ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java
Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java?rev=902342&r1=902341&r2=902342&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/test/BaseUnitTests.java Sat Jan 23 05:39:30 2010
@@ -21,6 +21,7 @@
import junit.framework.TestCase;
import org.ofbiz.base.conversion.*;
+import org.ofbiz.base.util.ComparableRange;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilFormatOut;
import org.ofbiz.base.util.UtilValidate;
@@ -72,6 +73,27 @@
assertTrue(UtilValidate.isFloat("10.000", true, true, 3, 3));
}
+ public void testComparableRange() {
+ ComparableRange<Integer> pointTest = new ComparableRange<Integer>(1, 1);
+ assertTrue("isPoint", pointTest.isPoint());
+ assertTrue("equality", pointTest.equals(new ComparableRange<Integer>(1, 1)));
+ ComparableRange<Integer> range1 = new ComparableRange<Integer>(3, 1);
+ ComparableRange<Integer> range2 = new ComparableRange<Integer>(4, 6);
+ assertTrue("after range", range2.after(range1));
+ assertTrue("before range", range1.before(range2));
+ assertFalse("excludes value", range1.includes(0));
+ assertTrue("includes value", range1.includes(1));
+ assertTrue("includes value", range1.includes(2));
+ assertTrue("includes value", range1.includes(3));
+ assertFalse("excludes value", range1.includes(4));
+ assertTrue("includes range", range1.includes(pointTest));
+ assertFalse("excludes range", range1.includes(range2));
+ ComparableRange<Integer> overlapTest = new ComparableRange<Integer>(2, 5);
+ assertTrue("overlaps range", range1.overlaps(overlapTest));
+ assertTrue("overlaps range", range2.overlaps(overlapTest));
+ assertFalse("overlaps range", range1.overlaps(range2));
+ }
+
public void testDateTimeConverters() {
// Source class = java.util.Date
java.util.Date utilDate = new java.util.Date();
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ComparableRange.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ComparableRange.java?rev=902342&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ComparableRange.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ComparableRange.java Sat Jan 23 05:39:30 2010
@@ -0,0 +1,115 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.ofbiz.base.util;
+
+/** An immutable range of values. */
+public class ComparableRange<T> implements Range<T> {
+
+ @SuppressWarnings("unchecked")
+ protected static <T> Comparable<T> cast(T value) {
+ return (Comparable<T>) value;
+ }
+
+ protected final T start;
+ protected final T end;
+ protected final boolean isPoint;
+
+ @SuppressWarnings("unchecked")
+ public ComparableRange(Comparable<T> start, Comparable<T> end) {
+ if (end.compareTo((T) start) >= 0) {
+ this.start = (T) start;
+ this.end = (T) end;
+ } else {
+ this.start = (T) end;
+ this.end = (T) start;
+ }
+ this.isPoint = start.equals(end);
+ }
+
+ @Override
+ public boolean after(Range<T> range) {
+ return cast(this.start).compareTo(range.end()) > 0;
+ }
+
+ @Override
+ public boolean after(T value) {
+ return cast(this.start).compareTo(value) > 0;
+ }
+
+ @Override
+ public boolean before(Range<T> range) {
+ return cast(this.end).compareTo(range.start()) < 0;
+ }
+
+ @Override
+ public boolean before(T value) {
+ return cast(this.end).compareTo(value) < 0;
+ }
+
+ @Override
+ public T end() {
+ return this.end;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ try {
+ ComparableRange<T> that = (ComparableRange<T>) obj;
+ return this.start.equals(that.start) && this.end.equals(that.end);
+ } catch (ClassCastException e) {}
+ return false;
+ }
+
+ @Override
+ public boolean includes(Range<T> range) {
+ return this.includes(range.start()) && this.includes(range.end());
+ }
+
+ @Override
+ public boolean includes(T value) {
+ if (this.isPoint) {
+ return value.equals(this.start);
+ }
+ return (cast(value).compareTo(this.start) >= 0 && cast(value).compareTo(this.end) <= 0);
+ }
+
+ @Override
+ public boolean isPoint() {
+ return this.isPoint;
+ }
+
+ @Override
+ public boolean overlaps(Range<T> range) {
+ return range.includes(this.start) || range.includes(this.end) || this.includes(range);
+ }
+
+ @Override
+ public T start() {
+ return this.start;
+ }
+
+ @Override
+ public String toString() {
+ return this.start + " - " + this.end;
+ }
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ComparableRange.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ComparableRange.java
------------------------------------------------------------------------------
svn:keywords = Date Rev Author URL Id
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ComparableRange.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Range.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Range.java?rev=902342&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Range.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Range.java Sat Jan 23 05:39:30 2010
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.ofbiz.base.util;
+
+/** A range of values. */
+public interface Range<T> {
+ /** Returns <code>true</code> if the lowest value in this range
+ * occurs after the greatest value in <code>range</code>.
+ * @param range The range to test
+ * @return <code>true</code> if the lowest value in this range
+ * occurs after the greatest value in <code>range</code>
+ */
+ boolean after(Range<T> range);
+
+ /** Returns <code>true</code> if this range occurs after <code>value</code>.
+ * @param date
+ * @return <code>true</code> if this range occurs after <code>value</code>
+ */
+ boolean after(T value);
+
+ /** Returns <code>true</code> if the greatest value in this range
+ * occurs before the lowest value in <code>range</code>.
+ * @param range The range to test
+ * @return <code>true</code> if the greatest value in this range
+ * occurs before the lowest value in <code>range</code>
+ */
+ boolean before(Range<T> range);
+
+ /** Returns <code>true</code> if this range occurs before <code>value</code>.
+ * @param value The value to test
+ * @return <code>true</code> if this range occurs before <code>value</code>
+ */
+ boolean before(T value);
+
+ /** Returns the ending value of this range.
+ * @return Ending value
+ */
+ T end();
+
+ /** Returns <code>true</code> if this range includes <code>range</code>.
+ * @param range The range to test
+ * @return <code>true</code> if this range includes <code>range</code>
+ */
+ boolean includes(Range<T> range);
+
+ /** Returns <code>true</code> if <code>date</code> occurs within this range.
+ * @param value The value to test
+ * @return <code>true</code> if <code>value</code> occurs within this range
+ */
+ boolean includes(T value);
+
+ /** Returns <code>true</code> if the starting and ending values are equal.
+ * @return <code>true</code> if the starting and ending values are equal
+ */
+ boolean isPoint();
+
+ /** Returns <code>true</code> if this range overlaps <code>range</code>.
+ * @param range The range to test
+ * @return <code>true</code> if this range overlaps <code>range</code>
+ */
+ boolean overlaps(Range<T> range);
+
+ /** Returns the starting value of this range.
+ * @return Starting value
+ */
+ T start();
+
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Range.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Range.java
------------------------------------------------------------------------------
svn:keywords = Date Rev Author URL Id
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Range.java
------------------------------------------------------------------------------
svn:mime-type = text/plain