]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/Limit.java
Re-implement URIStringUtils escape and unescape using Unicode
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / Limit.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.util;
13
14 import org.simantics.databoard.annotations.Union;\r
15
16 @Union({
17         Limit.Nolimit.class,
18         Limit.Inclusive.class, Limit.Exclusive.class,
19         Limit.InclusiveLong.class, Limit.ExclusiveLong.class
20 })
21 public abstract class Limit {
22
23         private static Limit NOLIMIT = new Nolimit();
24
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); }    
36         
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();
42         Limit() {}
43         
44         public static class Nolimit extends Limit {
45                 public Nolimit() {}
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; }
51         }
52         
53         public static class Inclusive extends Limit {
54                 public Double value;            
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());    
62                 }
63                 public Integer smallestIncludedInteger() { 
64                         return (int)Math.ceil(value.doubleValue());     
65                 }
66         }
67
68         public static class Exclusive extends Limit {
69                 public Double value;            
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());  
77                 }
78                 public Integer smallestIncludedInteger() { 
79                         return 1+(int)Math.floor(value.doubleValue());  
80                 }
81         }
82         public static class InclusiveLong extends Limit {
83                 public Long value;              
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();        
91                 }
92                 public Integer smallestIncludedInteger() { 
93                         return value.intValue();        
94                 }
95         }
96
97         public static class ExclusiveLong extends Limit {
98                 public Long value;              
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;      
106                 }
107                 public Integer smallestIncludedInteger() { 
108                         return value.intValue()+1;      
109                 }
110         }
111
112         /**
113          * Compares two limits for equality.
114          * 
115          * Integer and flot are equal, e.g. [5 is equal to [5.0
116          */
117         @Override
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 );
125         }
126         
127         @Override
128         public String toString() {
129                 return getValue().toString();
130         }       
131         
132 }
133