use of java.util.SortedSet in project groovy-core by groovy.
the class StructuredSyntaxDocumentFilter method getMultiLineRun.
// given an offset, return the mlr it resides in
private MultiLineRun getMultiLineRun(int offset) {
MultiLineRun ml = null;
if (offset > 0) {
Integer os = Integer.valueOf(offset);
SortedSet set = mlTextRunSet.headSet(os);
if (!set.isEmpty()) {
ml = (MultiLineRun) set.last();
ml = ml.end() >= offset ? ml : null;
}
}
return ml;
}
use of java.util.SortedSet in project druid by druid-io.
the class UniformGranularityTest method testPeriodSegmentGranularity.
@Test
public void testPeriodSegmentGranularity() {
final GranularitySpec spec = new UniformGranularitySpec(new PeriodGranularity(new Period("P1D"), null, DateTimeZone.forID("America/Los_Angeles")), null, Lists.newArrayList(new Interval("2012-01-08T00-08:00/2012-01-11T00-08:00"), new Interval("2012-01-07T00-08:00/2012-01-08T00-08:00"), new Interval("2012-01-03T00-08:00/2012-01-04T00-08:00"), new Interval("2012-01-01T00-08:00/2012-01-03T00-08:00"), new Interval("2012-09-01T00-07:00/2012-09-03T00-07:00")));
Assert.assertTrue(spec.bucketIntervals().isPresent());
final Optional<SortedSet<Interval>> sortedSetOptional = spec.bucketIntervals();
final SortedSet<Interval> intervals = sortedSetOptional.get();
ArrayList<Long> actualIntervals = new ArrayList<>();
for (Interval interval : intervals) {
actualIntervals.add(interval.toDurationMillis());
}
final ISOChronology chrono = ISOChronology.getInstance(DateTimeZone.forID("America/Los_Angeles"));
final ArrayList<Long> expectedIntervals = Lists.newArrayList(new Interval("2012-01-01/2012-01-02", chrono).toDurationMillis(), new Interval("2012-01-02/2012-01-03", chrono).toDurationMillis(), new Interval("2012-01-03/2012-01-04", chrono).toDurationMillis(), new Interval("2012-01-07/2012-01-08", chrono).toDurationMillis(), new Interval("2012-01-08/2012-01-09", chrono).toDurationMillis(), new Interval("2012-01-09/2012-01-10", chrono).toDurationMillis(), new Interval("2012-01-10/2012-01-11", chrono).toDurationMillis(), new Interval("2012-09-01/2012-09-02", chrono).toDurationMillis(), new Interval("2012-09-02/2012-09-03", chrono).toDurationMillis());
Assert.assertEquals(expectedIntervals, actualIntervals);
}
use of java.util.SortedSet in project buck by facebook.
the class AuditInputCommand method printJsonInputs.
@VisibleForTesting
int printJsonInputs(final CommandRunnerParams params, TargetGraph graph) throws IOException {
final SortedMap<String, ImmutableSortedSet<Path>> targetToInputs = new TreeMap<>();
new AbstractBottomUpTraversal<TargetNode<?, ?>, RuntimeException>(graph) {
@Override
public void visit(TargetNode<?, ?> node) {
Optional<Cell> cellRoot = params.getCell().getCellIfKnown(node.getBuildTarget());
Cell cell = cellRoot.isPresent() ? cellRoot.get() : params.getCell();
LOG.debug("Looking at inputs for %s", node.getBuildTarget().getFullyQualifiedName());
SortedSet<Path> targetInputs = new TreeSet<>();
for (Path input : node.getInputs()) {
LOG.debug("Walking input %s", input);
try {
if (!cell.getFilesystem().exists(input)) {
throw new HumanReadableException("Target %s refers to non-existent input file: %s", node, params.getCell().getRoot().relativize(cell.getRoot().resolve(input)));
}
targetInputs.addAll(cell.getFilesystem().getFilesUnderPath(input));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
targetToInputs.put(node.getBuildTarget().getFullyQualifiedName(), ImmutableSortedSet.copyOf(targetInputs));
}
}.traverse();
params.getObjectMapper().writeValue(params.getConsole().getStdOut(), targetToInputs);
return 0;
}
use of java.util.SortedSet in project hudson-2.x by hudson.
the class DeepEquals method deepEquals.
/**
* Compare two objects with a 'deep' comparison. This will traverse the
* Object graph and perform either a field-by-field comparison on each
* object (if no .equals() method has been overridden from Object), or it
* will call the customized .equals() method if it exists. This method will
* allow object graphs loaded at different times (with different object ids)
* to be reliably compared. Object.equals() / Object.hashCode() rely on the
* object's identity, which would not consider to equivalent objects necessarily
* equals. This allows graphs containing instances of Classes that did no
* overide .equals() / .hashCode() to be compared. For example, testing for
* existence in a cache. Relying on an objects identity will not locate an
* object in cache, yet relying on it being equivalent will.<br/><br/>
*
* This method will handle cycles correctly, for example A->B->C->A. Suppose a and
* a' are two separate instances of the A with the same values for all fields on
* A, B, and C. Then a.deepEquals(a') will return true. It uses cycle detection
* storing visited objects in a Set to prevent endless loops.
* @param a Object one to compare
* @param b Object two to compare
* @return true if a is equivalent to b, false otherwise. Equivalent means that
* all field values of both subgraphs are the same, either at the field level
* or via the respectively encountered overridden .equals() methods during
* traversal.
*/
public static boolean deepEquals(Object a, Object b) {
Set visited = new HashSet<DualKey>();
LinkedList<DualKey> stack = new LinkedList<DualKey>();
stack.addFirst(new DualKey(a, b));
while (!stack.isEmpty()) {
DualKey dualKey = stack.removeFirst();
visited.add(dualKey);
if (dualKey._key1 == dualKey._key2) {
// Same instance is always equal to itself.
continue;
}
if (dualKey._key1 == null || dualKey._key2 == null) {
// If either one is null, not equal (both can't be null, due to above comparison).
return false;
}
if (!dualKey._key1.getClass().equals(dualKey._key2.getClass())) {
// Must be same class
return false;
}
// the array must be deeply equivalent.
if (dualKey._key1.getClass().isArray()) {
if (!compareArrays(dualKey._key1, dualKey._key2, stack, visited)) {
return false;
}
continue;
}
// elements must be in the same order to be equivalent Sets.
if (dualKey._key1 instanceof SortedSet) {
if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) {
return false;
}
continue;
}
// be assumed, a temporary Map must be created, however the comparison still runs in O(N) time.
if (dualKey._key1 instanceof Set) {
if (!compareUnorderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) {
return false;
}
continue;
}
// matters, therefore this comparison is faster than using unordered comparison.
if (dualKey._key1 instanceof Collection) {
if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) {
return false;
}
continue;
}
// Maps can be compared in O(N) time due to their ordering.
if (dualKey._key1 instanceof SortedMap) {
if (!compareSortedMap((SortedMap) dualKey._key1, (SortedMap) dualKey._key2, stack, visited)) {
return false;
}
continue;
}
// comparison still runs in O(N) time.
if (dualKey._key1 instanceof Map) {
if (!compareUnorderedMap((Map) dualKey._key1, (Map) dualKey._key2, stack, visited)) {
return false;
}
continue;
}
if (hasCustomEquals(dualKey._key1.getClass())) {
if (!dualKey._key1.equals(dualKey._key2)) {
return false;
}
continue;
}
Collection<Field> fields = getDeepDeclaredFields(dualKey._key1.getClass());
for (Field field : fields) {
try {
DualKey dk = new DualKey(field.get(dualKey._key1), field.get(dualKey._key2));
if (!visited.contains(dk)) {
stack.addFirst(dk);
}
} catch (Exception ignored) {
}
}
}
return true;
}
use of java.util.SortedSet in project hibernate-orm by hibernate.
the class FooBarTest method testBatchLoad.
@Test
public void testBatchLoad() throws Exception {
Session s = openSession();
s.beginTransaction();
Baz baz = new Baz();
SortedSet stringSet = new TreeSet();
stringSet.add("foo");
stringSet.add("bar");
Set fooSet = new HashSet();
for (int i = 0; i < 3; i++) {
Foo foo = new Foo();
s.save(foo);
fooSet.add(foo);
}
baz.setFooSet(fooSet);
baz.setStringSet(stringSet);
s.save(baz);
Baz baz2 = new Baz();
fooSet = new HashSet();
for (int i = 0; i < 2; i++) {
Foo foo = new Foo();
s.save(foo);
fooSet.add(foo);
}
baz2.setFooSet(fooSet);
s.save(baz2);
Baz baz3 = new Baz();
stringSet = new TreeSet();
stringSet.add("foo");
stringSet.add("baz");
baz3.setStringSet(stringSet);
s.save(baz3);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
baz = (Baz) s.load(Baz.class, baz.getCode());
baz2 = (Baz) s.load(Baz.class, baz2.getCode());
baz3 = (Baz) s.load(Baz.class, baz3.getCode());
assertFalse(Hibernate.isInitialized(baz.getFooSet()) || Hibernate.isInitialized(baz2.getFooSet()) || Hibernate.isInitialized(baz3.getFooSet()));
assertFalse(Hibernate.isInitialized(baz.getStringSet()) || Hibernate.isInitialized(baz2.getStringSet()) || Hibernate.isInitialized(baz3.getStringSet()));
assertTrue(baz.getFooSet().size() == 3);
assertTrue(Hibernate.isInitialized(baz.getFooSet()) && Hibernate.isInitialized(baz2.getFooSet()) && Hibernate.isInitialized(baz3.getFooSet()));
assertTrue(baz2.getFooSet().size() == 2);
assertTrue(baz3.getStringSet().contains("baz"));
assertTrue(Hibernate.isInitialized(baz.getStringSet()) && Hibernate.isInitialized(baz2.getStringSet()) && Hibernate.isInitialized(baz3.getStringSet()));
assertTrue(baz.getStringSet().size() == 2 && baz2.getStringSet().size() == 0);
s.delete(baz);
s.delete(baz2);
s.delete(baz3);
Iterator iter = new JoinedIterator(new Iterator[] { baz.getFooSet().iterator(), baz2.getFooSet().iterator() });
while (iter.hasNext()) s.delete(iter.next());
s.getTransaction().commit();
s.close();
}
Aggregations