001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-api/api/src/java/org/sakaiproject/metaobj/utils/xml/ValueRange.java $
003: * $Id: ValueRange.java 8052 2006-04-20 18:16:21Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.xml;
021:
022: /**
023: * Created by IntelliJ IDEA.
024: * User: John Ellis
025: * Date: Aug 19, 2005
026: * Time: 4:20:00 PM
027: * To change this template use File | Settings | File Templates.
028: */
029:
030: /**
031: * Class that holds information about the range restrictions that
032: * apply to an element value.
033: */
034: public class ValueRange {
035:
036: private Comparable max = null;
037: private Comparable min = null;
038:
039: private boolean maxInclusive = false;
040: private boolean minInclusive = false;
041:
042: /**
043: * construct a new one
044: *
045: * @param max the max comparable object, null if there is no upper bound
046: * @param min the min comparable object, null if there is no lower bound
047: * @param maxInclusive true if value can equal max
048: * @param minInclusive true if value can equal min
049: */
050: public ValueRange(Comparable max, Comparable min,
051: boolean maxInclusive, boolean minInclusive) {
052: this .max = max;
053: this .min = min;
054: this .maxInclusive = maxInclusive;
055: this .minInclusive = minInclusive;
056: }
057:
058: /**
059: * test if the current value is in this range
060: *
061: * @param value value to test
062: * @return true if the supplied value is in range
063: */
064: public boolean inRange(Comparable value) {
065: if (max == null && min == null) {
066: return true;
067: }
068:
069: if (max == null) {
070: return compartMin(value);
071: } else if (min == null) {
072: return compareMax(value);
073: } else {
074: return compareMax(value) && compartMin(value);
075: }
076: }
077:
078: protected boolean compareMax(Comparable value) {
079: int result = value.compareTo(max);
080: return (maxInclusive ? result <= 0 : result < 0);
081: }
082:
083: protected boolean compartMin(Comparable value) {
084: int result = value.compareTo(min);
085: return (minInclusive ? result >= 0 : result > 0);
086: }
087:
088: public Comparable getMax() {
089: return max;
090: }
091:
092: public void setMax(Comparable max) {
093: this .max = max;
094: }
095:
096: public Comparable getMin() {
097: return min;
098: }
099:
100: public void setMin(Comparable min) {
101: this .min = min;
102: }
103:
104: public boolean isMaxInclusive() {
105: return maxInclusive;
106: }
107:
108: public void setMaxInclusive(boolean maxInclusive) {
109: this .maxInclusive = maxInclusive;
110: }
111:
112: public boolean isMinInclusive() {
113: return minInclusive;
114: }
115:
116: public void setMinInclusive(boolean minInclusive) {
117: this.minInclusive = minInclusive;
118: }
119:
120: }
|