org.modeshape.jcr.value.ValueComparators Maven / Gradle / Ivy
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you 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.
*
* ModeShape 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.jcr.value;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URI;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.UUID;
import javax.jcr.RepositoryException;
import org.modeshape.common.annotation.Immutable;
import org.modeshape.common.util.SecureHash;
import org.modeshape.jcr.GraphI18n;
import org.modeshape.jcr.api.value.DateTime;
import org.modeshape.jcr.value.basic.StringValueFactory;
/**
* A set of {@link Comparator} objects for the different kinds of property values.
*
* @see PropertyType#getComparator()
*/
@Immutable
public class ValueComparators {
/**
* A comparator of string values.
*/
public static final Comparator STRING_COMPARATOR = new Comparator() {
@Override
public int compare( String o1,
String o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of long values.
*/
public static final Comparator LONG_COMPARATOR = new Comparator() {
@Override
public int compare( Long o1,
Long o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of double values.
*/
public static final Comparator DOUBLE_COMPARATOR = new Comparator() {
@Override
public int compare( Double o1,
Double o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of decimal values.
*/
public static final Comparator DECIMAL_COMPARATOR = new Comparator() {
@Override
public int compare( BigDecimal o1,
BigDecimal o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of binary values. Although {@link BinaryValue} is {@link Comparable}, this comparator does not rely upon any
* particular Binary implementation. Thus, Binary implementations can use this for their {@link Comparable#compareTo(Object)}
* implementation.
*/
public static final Comparator BINARY_COMPARATOR = new Comparator() {
@Override
public int compare( BinaryValue o1,
BinaryValue o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
try {
try {
// Check the lengths first ...
final long len1 = o1.getSize();
final long len2 = o2.getSize();
if (len1 < len2) return -1;
if (len1 > len2) return 1;
// Compare using the hashes, if available
String hash1 = o1.getHexHash();
String hash2 = o2.getHexHash();
if (hash1 != null && hash2 != null) {
// If the hashes match, then we should assume that the values match.
// That's the whole point of using a secure hash.
return hash1.compareTo(hash2);
}
// One or both of the hashes could not be generated, so we have to go compare
// the whole values. This is unfortunate, but should happen very rarely (if ever)
// as long as the BinaryValue.getHash() is always implemented
// Otherwise they are the same length ...
InputStream stream1 = null;
InputStream stream2 = null;
try {
stream1 = o1.getStream();
stream2 = o2.getStream();
byte[] buffer1 = new byte[1024];
byte[] buffer2 = new byte[1024];
while (true) {
int numRead1 = stream1.read(buffer1);
if (numRead1 < 0) break;
int numRead2 = stream2.read(buffer2);
if (numRead1 != numRead2) {
throw new IoException(GraphI18n.errorReadingPropertyValueBytes.text());
}
for (int i = 0; i != numRead1; ++i) {
int diff = buffer1[i] - buffer2[i];
if (diff != 0) return diff;
}
}
return 0;
} catch (RepositoryException e) {
throw new IoException(GraphI18n.errorReadingPropertyValueBytes.text());
} catch (IOException e) {
throw new IoException(GraphI18n.errorReadingPropertyValueBytes.text());
} finally {
if (stream1 != null) {
try {
stream1.close();
} catch (IOException e) {
// do nothing
}
}
if (stream2 != null) {
try {
stream2.close();
} catch (IOException e) {
// do nothing
}
}
}
} finally {
o2.dispose();
}
} finally {
o1.dispose();
}
}
};
/**
* A comparator of boolean values.
*/
public static final Comparator BOOLEAN_COMPARATOR = new Comparator() {
@Override
public int compare( Boolean o1,
Boolean o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of date-time instances.
*/
public static final Comparator DATE_TIME_COMPARATOR = new Comparator() {
@Override
public int compare( DateTime o1,
DateTime o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of date values.
*/
public static final Comparator DATE_COMPARATOR = new Comparator() {
@Override
public int compare( Date o1,
Date o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of calendar values.
*/
public static final Comparator CALENDAR_COMPARATOR = new Comparator() {
@Override
public int compare( Calendar o1,
Calendar o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of name values.
*/
public static final Comparator NAME_COMPARATOR = new Comparator() {
@Override
public int compare( Name o1,
Name o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of path values.
*/
public static final Comparator PATH_COMPARATOR = new Comparator() {
@Override
public int compare( Path o1,
Path o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of path segment values.
*/
public static final Comparator PATH_SEGMENT_COMPARATOR = new Comparator() {
@Override
public int compare( Path.Segment o1,
Path.Segment o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of path segment names, excluding same-name-sibling indexes.
*/
public static final Comparator PATH_SEGMENT_NAME_COMPARATOR = new Comparator() {
@Override
public int compare( Path.Segment o1,
Path.Segment o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.getName().compareTo(o2.getName());
}
};
/**
* A comparator of URI values.
*/
public static final Comparator URI_COMPARATOR = new Comparator() {
@Override
public int compare( URI o1,
URI o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of UUID values.
*/
public static final Comparator UUID_COMPARATOR = new Comparator() {
@Override
public int compare( UUID o1,
UUID o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of reference values.
*/
public static final Comparator REFERENCE_COMPARATOR = new Comparator() {
@Override
public int compare( Reference o1,
Reference o2 ) {
if (o1 == o2) return 0;
if (o1 == null) return -1;
if (o2 == null) return 1;
return o1.compareTo(o2);
}
};
/**
* A comparator of other values.
*/
public static final Comparator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy