1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.util;
14 import org.simantics.databoard.annotations.Union;
18 Limit.Inclusive.class, Limit.Exclusive.class,
19 Limit.InclusiveLong.class, Limit.ExclusiveLong.class
21 public abstract class Limit {
23 private static Limit NOLIMIT = new Nolimit();
25 public static Limit nolimit() { return NOLIMIT; }
26 public static Limit inclusive(Byte value) { return value==null ? NOLIMIT : new InclusiveLong(value.longValue()); }
27 public static Limit inclusive(Integer value) { return value==null ? NOLIMIT : new InclusiveLong(value.longValue()); }
28 public static Limit inclusive(Long value) { return value==null ? NOLIMIT : new InclusiveLong(value); }
29 public static Limit inclusive(Float value) { return value==null ? NOLIMIT : new Inclusive(value.doubleValue()); }
30 public static Limit inclusive(Double value) { return value==null ? NOLIMIT : new Inclusive(value); }
31 public static Limit exclusive(Byte value) { return value==null ? NOLIMIT : new ExclusiveLong(value.longValue()); }
32 public static Limit exclusive(Integer value) { return value==null ? NOLIMIT : new ExclusiveLong(value.longValue()); }
33 public static Limit exclusive(Long value) { return value==null ? NOLIMIT : new ExclusiveLong(value); }
34 public static Limit exclusive(Float value) { return value==null ? NOLIMIT : new Exclusive(value.doubleValue()); }
35 public static Limit exclusive(Double value) { return value==null ? NOLIMIT : new Exclusive(value); }
37 public abstract boolean isInclusive();
38 public abstract boolean isExclusive();
39 public abstract Number getValue();
40 public abstract Integer smallestIncludedInteger();
41 public abstract Integer greatestIncludedInteger();
44 public static class Nolimit extends Limit {
46 public Number getValue() {return null;}
47 public boolean isExclusive() {return false;}
48 public boolean isInclusive() {return false;}
49 public Integer greatestIncludedInteger() { return null; }
50 public Integer smallestIncludedInteger() { return null; }
53 public static class Inclusive extends Limit {
55 public Inclusive(Double value) { this.value = value; }
56 public Number getValue() { return value; }
57 public boolean isExclusive() { return false; }
58 public boolean isInclusive() { return true; }
59 public int hashCode() { return value.hashCode() ^ -1; }
60 public Integer greatestIncludedInteger() {
61 return (int)Math.floor(value.doubleValue());
63 public Integer smallestIncludedInteger() {
64 return (int)Math.ceil(value.doubleValue());
68 public static class Exclusive extends Limit {
70 public Exclusive(Double value) { this.value = value; }
71 public Number getValue() { return value; }
72 public boolean isExclusive() { return true; }
73 public boolean isInclusive() { return false; }
74 public int hashCode() { return value.hashCode(); }
75 public Integer greatestIncludedInteger() {
76 return -1+(int)Math.ceil(value.doubleValue());
78 public Integer smallestIncludedInteger() {
79 return 1+(int)Math.floor(value.doubleValue());
82 public static class InclusiveLong extends Limit {
84 public InclusiveLong(Long value) { this.value = value; }
85 public Number getValue() { return value; }
86 public boolean isExclusive() { return false; }
87 public boolean isInclusive() { return true; }
88 public int hashCode() { return new Double(value.doubleValue()).hashCode() ^ -1; }
89 public Integer greatestIncludedInteger() {
90 return value.intValue();
92 public Integer smallestIncludedInteger() {
93 return value.intValue();
97 public static class ExclusiveLong extends Limit {
99 public ExclusiveLong(Long value) { this.value = value; }
100 public Number getValue() { return value; }
101 public boolean isExclusive() { return true; }
102 public boolean isInclusive() { return false; }
103 public int hashCode() { return new Double(value.doubleValue()).hashCode(); }
104 public Integer greatestIncludedInteger() {
105 return value.intValue()-1;
107 public Integer smallestIncludedInteger() {
108 return value.intValue()+1;
113 * Compares two limits for equality.
115 * Integer and flot are equal, e.g. [5 is equal to [5.0
118 public boolean equals(Object obj) {
119 if (obj instanceof Limit == false) return false;
120 Limit other = (Limit) obj;
121 Number ln = getValue();
122 Number on = other.getValue();
123 return isInclusive()==other.isInclusive() && isExclusive()==other.isExclusive() &&
124 ( ln==null ? on==null : on==null ? false : NumberComparator.INSTANCE.compare(ln, on)==0 );
128 public String toString() {
129 return getValue().toString();