Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 21 additions & 42 deletions src/main/java/com/metamx/common/guava/BaseSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package com.metamx.common.guava;

import com.google.common.base.Throwables;
import com.metamx.common.logger.Logger;

import java.io.Closeable;
import java.io.IOException;
import java.util.Iterator;
Expand All @@ -27,30 +24,9 @@
*/
public class BaseSequence<T, IterType extends Iterator<T>> implements Sequence<T>
{
private static final Logger log = new Logger(BaseSequence.class);

private final IteratorMaker<T, IterType> maker;

public static <T> Sequence<T> simple(final Iterable<T> iterable)
{
return new BaseSequence<>(
new BaseSequence.IteratorMaker<T, Iterator<T>>()
{
@Override
public Iterator<T> make()
{
return iterable.iterator();
}

@Override
public void cleanup(Iterator<T> iterFromMake)
{

}
}
);
}

public BaseSequence(
IteratorMaker<T, IterType> maker
)
Expand All @@ -66,11 +42,18 @@ public <OutType> OutType accumulate(OutType initValue, final Accumulator<OutType
while (iterator.hasNext()) {
initValue = fn.accumulate(initValue, iterator.next());
}
return initValue;
}
finally {
maker.cleanup(iterator);
catch (Throwable t) {
try {
maker.cleanup(iterator);
}
catch (Exception e) {
t.addSuppressed(e);
}
throw t;
}
maker.cleanup(iterator);
return initValue;
}

@Override
Expand All @@ -81,16 +64,14 @@ public <OutType> Yielder<OutType> toYielder(OutType initValue, YieldingAccumulat
try {
return makeYielder(initValue, accumulator, iterator);
}
catch (Exception e) {
// We caught an Exception instead of returning a really, real, live, real boy, errr, iterator
// So we better try to close our stuff, 'cause the exception is what is making it out of here.
catch (Throwable t) {
try {
maker.cleanup(iterator);
}
catch (RuntimeException e1) {
log.error(e1, "Exception thrown when closing maker. Logging and ignoring.");
catch (Exception e) {
t.addSuppressed(e);
}
throw Throwables.propagate(e);
throw t;
}
}

Expand Down Expand Up @@ -135,16 +116,14 @@ public Yielder<OutType> next(OutType initValue)
try {
return makeYielder(initValue, accumulator, iter);
}
catch (Exception e) {
// We caught an Exception instead of returning a really, real, live, real boy, errr, iterator
// So we better try to close our stuff, 'cause the exception is what is making it out of here.
catch (Throwable t) {
try {
maker.cleanup(iter);
}
catch (RuntimeException e1) {
log.error(e1, "Exception thrown when closing maker. Logging and ignoring.");
catch (Exception e) {
t.addSuppressed(e);
}
throw Throwables.propagate(e);
throw t;
}
}

Expand All @@ -162,10 +141,10 @@ public void close() throws IOException
};
}

public static interface IteratorMaker<T, IterType extends Iterator<T>>
public interface IteratorMaker<T, IterType extends Iterator<T>>
{
public IterType make();
IterType make();

public void cleanup(IterType iterFromMake);
void cleanup(IterType iterFromMake);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/metamx/common/guava/Comparators.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.metamx.common.guava;

import com.google.common.primitives.Longs;
import org.joda.time.DateTimeComparator;
import org.joda.time.Interval;

Expand Down Expand Up @@ -71,6 +72,13 @@ public int compare(T t, T t1)
@Override
public int compare(Interval lhs, Interval rhs)
{
if (lhs.getChronology().equals(rhs.getChronology())) {
int compare = Longs.compare(lhs.getStartMillis(), rhs.getStartMillis());
if (compare == 0) {
return Longs.compare(lhs.getEndMillis(), rhs.getEndMillis());
}
return compare;
}
int retVal = dateTimeComp.compare(lhs.getStart(), rhs.getStart());
if (retVal == 0) {
retVal = dateTimeComp.compare(lhs.getEnd(), rhs.getEnd());
Expand All @@ -86,6 +94,13 @@ public int compare(Interval lhs, Interval rhs)
@Override
public int compare(Interval lhs, Interval rhs)
{
if (lhs.getChronology().equals(rhs.getChronology())) {
int compare = Longs.compare(lhs.getEndMillis(), rhs.getEndMillis());
if (compare == 0) {
return Longs.compare(lhs.getStartMillis(), rhs.getStartMillis());
}
return compare;
}
int retVal = dateTimeComp.compare(lhs.getEnd(), rhs.getEnd());
if (retVal == 0) {
retVal = dateTimeComp.compare(lhs.getStart(), rhs.getStart());
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/metamx/common/guava/ConcatSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.metamx.common.guava;

import com.google.common.base.Throwables;
import com.google.common.io.Closeables;

import java.io.Closeable;
import java.io.IOException;

/**
Expand Down Expand Up @@ -71,11 +71,14 @@ public Sequence<T> accumulate(Sequence<T> accumulated, Sequence<T> in)
try {
return makeYielder(yielderYielder, initValue, accumulator);
}
catch (RuntimeException e) {
// We caught a RuntimeException instead of returning a really, real, live, real boy, errr, iterator
// So we better try to close our stuff, 'cause the exception is what is making it out of here.
CloseQuietly.close(yielderYielder);
throw e;
catch (Throwable t) {
try {
yielderYielder.close();
}
catch (Exception e) {
t.addSuppressed(e);
}
throw t;
}
}

Expand All @@ -85,10 +88,6 @@ public <OutType> Yielder<OutType> makeYielder(
YieldingAccumulator<OutType, T> accumulator
)
{
if (yielderYielder.isDone()) {
return Yielders.done(initValue, yielderYielder);
}

while (!yielderYielder.isDone()) {
Yielder<OutType> yielder = yielderYielder.get().toYielder(initValue, accumulator);
if (accumulator.yielded()) {
Expand Down Expand Up @@ -150,8 +149,9 @@ public boolean isDone()
@Override
public void close() throws IOException
{
yielder.close();
yielderYielder.close();
try (Closeable toClose = yielderYielder) {
yielder.close();
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ public boolean isDone()
@Override
public void close() throws IOException
{
if (isDone()) {
boolean done = isDone();
baseYielder.close();
if (done) {
executor.execute(runnable);
}
baseYielder.close();
}
}
138 changes: 0 additions & 138 deletions src/main/java/com/metamx/common/guava/ExecutorExecutingSequence.java

This file was deleted.

Loading