1/*2 * Copyright 2013 The Apache Software Foundation.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.apache.juddi.v3.client.compare;
1718import javax.xml.datatype.DatatypeConfigurationException;
19import javax.xml.datatype.DatatypeConstants;
20import javax.xml.datatype.DatatypeFactory;
21import javax.xml.datatype.Duration;
22import javax.xml.datatype.XMLGregorianCalendar;
23import org.uddi.api_v3.InstanceDetails;
24import org.uddi.api_v3.TModelInstanceDetails;
2526/**27 * Compares two UDDI TModelInstanceDetails, searching for a specific tModel key,28 * then parsing to the selected data type, then comparing.29 *30 * @author Alex O'Ree31 */32publicclassTModelInstanceDetailsComparator {
3334/**35 *36 * @param TModelKey for TModelInstanceInfo to use for comparison37 * @param number if true, the InstanceDetails.InstanceParms will be treated38 * like a number39 * @param XMLdate if true, the InstanceDetails.InstanceParms will be treated40 * like a XML Date41 * @param XMLduration if true, the InstanceDetails.InstanceParms will be42 * treated like a XML Gregorian Calendar43 * @see Duration44 * @see XMLGregorianCalendar45 * @throws DatatypeConfigurationException46 */47publicTModelInstanceDetailsComparator(String TModelKey, boolean number, boolean XMLdate, boolean XMLduration) throws DatatypeConfigurationException, IllegalArgumentException {
48if (TModelKey == null || TModelKey.length() == 0) {
49thrownew IllegalArgumentException();
50 }
51 compareField = TModelKey;
52if (!number && !XMLdate && !XMLduration) {
53thrownew IllegalArgumentException("only one data type can be selected");
54 }
55if (number && XMLdate && !XMLduration) {
56thrownew IllegalArgumentException("only one data type can be selected");
57 }
58if (number && !XMLdate && XMLduration) {
59thrownew IllegalArgumentException("only one data type can be selected");
60 }
61if (!number && XMLdate && XMLduration) {
62thrownew IllegalArgumentException("only one data type can be selected");
63 }
64if (number && XMLdate && XMLduration) {
65thrownew IllegalArgumentException("only one data type can be selected");
66 }
67 fac = DatatypeFactory.newInstance();
68 isNumber = number;
69 isDate = XMLdate;
70 isDuration = XMLduration;
71 }
72 DatatypeFactory fac = null;
73 String compareField = null;
74boolean isNumber = false;
75boolean isDate = false;
76boolean isDuration = false;
7778/**79 * Compares two non-null instances of TModelInstanceDetails by only80 * comparing the field designated from the constructor. It will also cast or81 * parse TModelInstanceDetails[i].InstanceDetails[k].InstanceParms to the82 * selected data type double, XMLGregorgian or Duration, using that as a83 * basis for comparison. If a parsing error occurs, an exception will be84 * thrown.85 *86 * @param lhs87 * @param rhs88 * @return less than 0 if lhs < rhs, greater than 0 if lhs > rhs.89 * @throws IllegalArgumentException if the tModel key to search for is90 * missing, if either sides are null91 * @throws ArrayIndexOutOfBoundsException if the values were found but could92 * not be compared93 */94publicint compare(TModelInstanceDetails lhs, TModelInstanceDetails rhs) throws IllegalArgumentException, NumberFormatException, NullPointerException, ArrayIndexOutOfBoundsException {
95if (lhs == null) {
96thrownew IllegalArgumentException("lhs");
97 }
98if (rhs == null) {
99thrownew IllegalArgumentException("rhs");
100 }
101if (lhs.getTModelInstanceInfo().isEmpty() || rhs.getTModelInstanceInfo().isEmpty()) {
102thrownew IllegalArgumentException("no data to compare");
103 }
104InstanceDetails lhsc = null;
105InstanceDetails rhsc = null;
106for (int i = 0; i < lhs.getTModelInstanceInfo().size(); i++) {
107if (lhs.getTModelInstanceInfo().get(i).getTModelKey().equalsIgnoreCase(compareField)) {
108 lhsc = lhs.getTModelInstanceInfo().get(i).getInstanceDetails();
109 }
110 }
111for (int i = 0; i < rhs.getTModelInstanceInfo().size(); i++) {
112if (rhs.getTModelInstanceInfo().get(i).getTModelKey().equalsIgnoreCase(compareField)) {
113 rhsc = rhs.getTModelInstanceInfo().get(i).getInstanceDetails();
114 }
115 }
116117if (lhsc == null) {
118thrownew IllegalArgumentException(compareField + " not found for lhs");
119 }
120if (rhsc == null) {
121thrownew IllegalArgumentException(compareField + " not found for rhs");
122 }
123if (lhsc.getInstanceParms() == null) {
124thrownew IllegalArgumentException(compareField + " found lhs, but no data");
125 }
126if (rhsc.getInstanceParms() == null) {
127thrownew IllegalArgumentException(compareField + " found rhs, but no data");
128 }
129if (isNumber) {
130 Double l = Double.parseDouble(lhsc.getInstanceParms());
131 Double r = Double.parseDouble(rhsc.getInstanceParms());
132return l.compareTo(r);
133 }
134135if (isDate) {
136 XMLGregorianCalendar l = fac.newXMLGregorianCalendar(lhsc.getInstanceParms());
137 XMLGregorianCalendar r = fac.newXMLGregorianCalendar(rhsc.getInstanceParms());
138//System.out.println(l.toXMLFormat() + " " + r.toXMLFormat());139int x = l.compare(r);
140141if (x == DatatypeConstants.LESSER) {
142return -1;
143 }
144145if (x == DatatypeConstants.GREATER) {
146return 1;
147 }
148if (x == DatatypeConstants.EQUAL) {
149return 0;
150 }
151thrownew ArrayIndexOutOfBoundsException("cannot compare, result was " + x);
152 }
153154if (isDuration) {
155 Duration l = fac.newDuration(lhsc.getInstanceParms());
156 Duration r = fac.newDuration(rhsc.getInstanceParms());
157// System.out.println(l.toString() + " " + r.toString());158int x = l.compare(r);
159160if (x == DatatypeConstants.LESSER) {
161return -1;
162 }
163164if (x == DatatypeConstants.GREATER) {
165return 1;
166 }
167if (x == DatatypeConstants.EQUAL) {
168return 0;
169 }
170thrownew ArrayIndexOutOfBoundsException("cannot compare, result was " + x);
171 }
172173return 0;
174 }
175 }