View Javadoc

1   /*	
2   	Copyright 2007-2014 Fraunhofer IGD, http://www.igd.fraunhofer.de
3   	
4   	See the NOTICE file distributed with this work for additional 
5   	information regarding copyright ownership
6   	
7   	Licensed under the Apache License, Version 2.0 (the "License");
8   	you may not use this file except in compliance with the License.
9   	You may obtain a copy of the License at
10  	
11  	  http://www.apache.org/licenses/LICENSE-2.0
12  	
13  	Unless required by applicable law or agreed to in writing, software
14  	distributed under the License is distributed on an "AS IS" BASIS,
15  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  	See the License for the specific language governing permissions and
17  	limitations under the License.
18   */
19  package org.universAAL.middleware.owl;
20  
21  /**
22   * Represents the class of individuals that can be compared to other individuals
23   * for finding out their (partial) order. The conventions described for the
24   * class hierarchy rooted at
25   * {@link org.universAAL.middleware.owl.ManagedIndividual}; in particular, the
26   * subclasses must override the following non-final static methods:
27   * {@link #getMaximum()} and {@link #getMinimum()}.
28   * 
29   * @author mtazari - <a href="mailto:Saied.Tazari@igd.fraunhofer.de">Saied
30   *         Tazari</a>
31   * @author Carsten Stockloew
32   */
33  public abstract class ComparableIndividual extends ManagedIndividual implements
34  	Comparable {
35      /**
36       * To be used in the implementation of
37       * {@link java.lang.Comparable#compareTo(Object)}, if the passed parameter
38       * is not comparable with the current individual.
39       */
40      public static final int NON_COMPARABLE_INDIVIDUALS = Integer.MAX_VALUE;
41  
42      public static final String MY_URI = uAAL_VOCABULARY_NAMESPACE
43  	    + "ComparableIndividual";
44  
45      protected ComparableIndividual() {
46  	super();
47      }
48  
49      protected ComparableIndividual(String uri) {
50  	super(uri);
51      }
52  
53      public String getClassURI() {
54  	return MY_URI;
55      }
56  
57      public static final ComparableIndividual getClassMaximum(Class claz) {
58  	// TODO: change to getMaxValue?
59  	try {
60  	    return (ComparableIndividual) claz.getMethod("getMaximum",
61  		    (Class[]) null).invoke(null, (Object[]) null);
62  	} catch (Exception e) {
63  	    return null;
64  	}
65      }
66  
67      public static final ComparableIndividual getClassMinimum(Class claz) {
68  	// TODO: change to getMinValue?
69  	try {
70  	    return (ComparableIndividual) claz.getMethod("getMinimum",
71  		    (Class[]) null).invoke(null, (Object[]) null);
72  	} catch (Exception e) {
73  	    return null;
74  	}
75      }
76  
77      /**
78       * Returns the "largest" instance in this class, if it exists.
79       */
80      public static ComparableIndividual getMaximum() {
81  	return null;
82      }
83  
84      /**
85       * Returns the "smallest" instance in this class, if it exists.
86       */
87      public static ComparableIndividual getMinimum() {
88  	return null;
89      }
90  
91      /**
92       * Compare this object to the given object.
93       * 
94       * @return <b>0</b>if current location and argument location are in the same
95       *         Place.<br>
96       *         <b>-1</b> if current location is in a Place contained in argument
97       *         location Place.<br>
98       *         <b>1</b> if argument location is in a Place contained in current
99       *         location Place.<br>
100      *         {@link ComparableIndividual#NON_COMPARABLE_INDIVIDUALS} if two
101      *         locations are not comparable.
102      */
103     public abstract int compareTo(Object arg0);
104 
105     public final ComparableIndividual getClassMaximum() {
106 	try {
107 	    return (ComparableIndividual) this.getClass()
108 		    .getMethod("getMaximum", (Class[]) null)
109 		    .invoke(null, (Object[]) null);
110 	} catch (Exception e) {
111 	    return null;
112 	}
113     }
114 
115     public final ComparableIndividual getClassMinimum() {
116 	try {
117 	    return (ComparableIndividual) this.getClass()
118 		    .getMethod("getMinimum", (Class[]) null)
119 		    .invoke(null, (Object[]) null);
120 	} catch (Exception e) {
121 	    return null;
122 	}
123     }
124 
125     /**
126      * Get the next value according to the total order of this comparable
127      * individual.
128      * 
129      * @return the next value
130      */
131     public abstract ComparableIndividual getNext();
132 
133     /**
134      * Get the previous value according to the total order of this comparable
135      * individual.
136      * 
137      * @return the previous value
138      */
139     public abstract ComparableIndividual getPrevious();
140 
141     /** Determines if this object equals the given object. */
142     public final boolean equal(Object other) {
143 	try {
144 	    return compareTo(other) == 0;
145 	} catch (Exception e) {
146 	    return false;
147 	}
148     }
149 
150     /** Determines if this object is greater than the given object. */
151     public final boolean greater(Object other) {
152 	try {
153 	    return compareTo(other) == 1;
154 	} catch (Exception e) {
155 	    return false;
156 	}
157     }
158 
159     /** Determines if this object is greater than or equals the given object. */
160     public final boolean greaterEqual(Object other) {
161 	try {
162 	    switch (compareTo(other)) {
163 	    case 0:
164 	    case 1:
165 		return true;
166 	    default:
167 		return false;
168 	    }
169 	} catch (Exception e) {
170 	    return false;
171 	}
172     }
173 
174     /** Determines if this object is smaller than the given object. */
175     public final boolean less(Object other) {
176 	try {
177 	    return compareTo(other) == -1;
178 	} catch (Exception e) {
179 	    return false;
180 	}
181     }
182 
183     /** Determines if this object is smaller than or equals the given object. */
184     public final boolean lessEqual(Object other) {
185 	try {
186 	    switch (compareTo(other)) {
187 	    case 0:
188 	    case -1:
189 		return true;
190 	    default:
191 		return false;
192 	    }
193 	} catch (Exception e) {
194 	    return false;
195 	}
196     }
197 
198     /**
199      * If there is a total strict order between the class members, then it must
200      * return the "serial number" of this instance, otherwise Integer.MIN_VALUE
201      * must be returned.
202      */
203     public abstract int ord();
204 
205 }