/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.model;
/**
* This class contains an enumeration of constants that define different
* types of number ranges.
*
* It implements the typesave enum pattern (Joshua Bloch, "Effective Java", Item 21).
* If you compare NumberRangeTypes, always use the == operator. This is
* save, because they are Singletons (Gamma et al., 1995).
*
* @author Markus Kraetzig
*/
public class NumberRangeTypes {
/**
* "(" (not inclusive) or "[" (inclusive).
*/
public final String leftBracket;
/**
* ")" (not inclusive) or "]" (inclusive).
*/
public final String rightBracket;
/**
* Whether lower bound is open.
*/
public final boolean leftOpen;
/**
* Whether upper bound is open.
*/
public final boolean rightOpen;
/**
* The name of the type.
*/
private final String name;
/**
* Type for closed interval.
*/
public static final NumberRangeTypes CLOSED =
new NumberRangeTypes("CLOSED", false, false);
/**
* Type for open interval.
*/
public static final NumberRangeTypes OPEN =
new NumberRangeTypes("OPEN", true, true);
/**
* Type for left-open interval.
*/
public static final NumberRangeTypes LEFT_OPEN =
new NumberRangeTypes("LEFT_OPEN", true, false);
/**
* Type for right-open interval.
*/
public static final NumberRangeTypes RIGHT_OPEN =
new NumberRangeTypes("RIGHT_OPEN", false, true);
/**
* NumberRangeTypes constructor with a descriptive name
* and the bounds settings.
*
* @param name a descriptive name
* @param leftOpen true if lower bound is open
* @param rightopen true if upper bound is open
*/
private NumberRangeTypes(
String name,
boolean leftOpen,
boolean rightOpen) {
this.name = name;
this.leftOpen = leftOpen;
this.rightOpen = rightOpen;
this.leftBracket = leftOpen ? "(" : "[";
this.rightBracket = rightOpen ? ")" : "]";
}
/**
* Gets the strimg for the left bracket.
*
* @return "(" or "["
*/
public final String leftBracket() {
return leftBracket;
}
/**
* Returns the name of the type. Do not use for comparisons,
* but compare by reference.
*
* @return name of this type
*/
public final String name() {
return name;
}
/**
* Gets the strimg for the right bracket.
*
* @return ")" or "]"
*/
public final String rightBracket() {
return rightBracket;
}
/**
* Gets the appropriate range type according to the specification
* of the bounds openess.
*
* @param leftOpen whether lower bound is open
* @param rightOpen whether upper bound is open
* @return number range type
*/
public static final NumberRangeTypes getTypeForBounds(
boolean leftOpen,
boolean rightOpen) {
if (leftOpen && rightOpen)
return OPEN;
if (!leftOpen && !rightOpen)
return CLOSED;
if (!leftOpen && rightOpen)
return RIGHT_OPEN;
if (leftOpen && !rightOpen)
return LEFT_OPEN;
throw new RuntimeException("Code should never be reached.");
}
/**
* Gets whether left bound is open.
*
* @return true if left bound is open
*/
public final boolean isLeftOpen() {
return leftOpen;
}
/**
* Gets whether right bound is open.
*
* @return true if right bound is open
*/
public final boolean isRightOpen() {
return rightOpen;
}
/**
* Returns a string representation of the type, do not use for comparisons.
*
* @return string representing this type
*/
public final String toString() {
return name + " " + leftBracket + "lower, upper" + rightBracket;
}
}
Ads help maintain this website.