Skip to content

Commit

Permalink
Merge logging
Browse files Browse the repository at this point in the history
see issue #19
  • Loading branch information
bertfrees committed Jun 19, 2015
2 parents 29141f5 + f258676 commit e008364
Show file tree
Hide file tree
Showing 49 changed files with 1,125 additions and 345 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import static com.google.common.collect.Iterables.find;

import org.osgi.service.component.ComponentContext;

Expand Down Expand Up @@ -67,7 +67,7 @@ private URI getExecutable(String name) {
possiblePaths.add(asURI("x86/" + fileName));
possiblePaths.add(asURI(os + "/x86/" + fileName)); }
try {
return Iterables.find(
return find(
possiblePaths,
new Predicate<URI>() { public boolean apply(URI p) { return resources.contains(p); }}); }
catch (NoSuchElementException e) { return null; }
Expand All @@ -81,7 +81,7 @@ private URI getSharedLibrary(String name) {
possiblePaths.add(asURI(arch + "/" + fileName));
possiblePaths.add(asURI(os + "/" + arch + "/" + fileName));
try {
return Iterables.find(
return find(
possiblePaths,
new Predicate<URI>() { public boolean apply(URI p) { return resources.contains(p); }}); }
catch (NoSuchElementException e) { return null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public URI getIdentifier() {
return identifier;
}

private final ResourceResolver resolver = new CachedResolver() {
public URL delegate(URI resource) {
private final ResourceResolver resolver = new MemoizingResolver() {
public URL _apply(URI resource) {
logger.trace("Resolving " + resource + " within " + identifier + " (real path: " + path + "; unpack dir: " + unpackDir + ")");
resource = resource.normalize();
if (resource.equals(identifier) || resource.equals(path) || resource.equals(unpackDir))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.daisy.pipeline.braille.common;

public interface Contextual<C,T> {

public T withContext(C context);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package org.daisy.pipeline.braille.common;

import org.slf4j.helpers.MarkerIgnoringBase;
import org.slf4j.helpers.MessageFormatter;
import org.slf4j.Logger;

public interface JobContext extends Logger {;

public abstract static class AbstractJobContext extends MarkerIgnoringBase implements JobContext {

private static final long serialVersionUID = 1L;

protected String format(String format, Object arg) {
return MessageFormatter.format(format, arg).getMessage();
}

protected String format(String format, Object arg1, Object arg2) {
return MessageFormatter.format(format, arg1, arg2).getMessage();
}

protected String format(String format, Object... args) {
return MessageFormatter.format(format, args).getMessage();
}

protected abstract void doTrace(String msg);

protected abstract void doDebug(String msg);

protected abstract void doInfo(String msg);

protected abstract void doWarn(String msg);

protected abstract void doError(String msg);

public void trace(String msg) {
if (isTraceEnabled())
doTrace(msg);
}

public void trace(String format, Object arg) {
if (isTraceEnabled())
doTrace(format(format, arg));
}

public void trace(String format, Object arg1, Object arg2) {
if (isTraceEnabled())
doTrace(format(format, arg1, arg2));
}

public void trace(String format, Object... args) {
if (isTraceEnabled())
doTrace(format(format, args));
}

public void trace(String msg, Throwable t) {
if (isTraceEnabled())
doTrace(msg);
}

public void debug(String msg) {
if (isDebugEnabled())
doDebug(msg);
}

public void debug(String format, Object arg) {
if (isDebugEnabled())
doDebug(format(format, arg));
}

public void debug(String format, Object arg1, Object arg2) {
if (isDebugEnabled())
doDebug(format(format, arg1, arg2));
}

public void debug(String format, Object... args) {
if (isDebugEnabled())
doDebug(format(format, args));
}

public void debug(String msg, Throwable t) {
if (isDebugEnabled())
doDebug(msg);
}

public void info(String msg) {
if (isInfoEnabled())
doInfo(msg);
}

public void info(String format, Object arg) {
if (isInfoEnabled())
doInfo(format(format, arg));
}

public void info(String format, Object arg1, Object arg2) {
if (isInfoEnabled())
doInfo(format(format, arg1, arg2));
}

public void info(String format, Object... args) {
if (isInfoEnabled())
doInfo(format(format, args));
}

public void info(String msg, Throwable t) {
if (isInfoEnabled())
doInfo(msg);
}

public void warn(String msg) {
if (isWarnEnabled())
doWarn(msg);
}

public void warn(String format, Object arg) {
if (isWarnEnabled())
doWarn(format(format, arg));
}

public void warn(String format, Object arg1, Object arg2) {
if (isWarnEnabled())
doWarn(format(format, arg1, arg2));
}

public void warn(String format, Object... args) {
if (isWarnEnabled())
doWarn(format(format, args));
}

public void warn(String msg, Throwable t) {
if (isWarnEnabled())
doWarn(msg);
}

public void error(String msg) {
if (isErrorEnabled())
doError(msg);
}

public void error(String format, Object arg) {
if (isErrorEnabled())
doError(format(format, arg));
}

public void error(String format, Object arg1, Object arg2) {
if (isErrorEnabled())
doError(format(format, arg1, arg2));
}

public void error(String format, Object... args) {
if (isErrorEnabled())
doError(format(format, args));
}

public void error(String msg, Throwable t) {
if (isErrorEnabled())
doError(msg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@

public abstract class LazyValue<V> implements Function0<V>, Iterable<V> {

public abstract V get();

public V apply() {
return get();
}

public Iterator<V> iterator() {
return new Iterator<V>() {
boolean hasNext = true;
Expand All @@ -23,18 +17,18 @@ public V next() {
if (!hasNext())
throw new NoSuchElementException();
hasNext = false;
return get();
return apply();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}

public static <V> LazyValue<V> from(final Function0<V> get) {
public static <V> LazyValue<V> from(final Function0<V> value) {
return new LazyValue<V>() {
public V get() {
return get.apply();
public V apply() {
return value.apply();
}
};
}
Expand All @@ -44,34 +38,19 @@ public static abstract class ImmutableLazyValue<V> extends LazyValue<V> {
private V value = null;
protected boolean computed = false;

public V get() {
public final V apply() {
if (!computed) {
value = delegate();
value = _apply();
computed = true; }
return value;
}

protected abstract V delegate();
protected abstract V _apply();

public static <V> LazyValue<V> from(final Function0<V> get) {
public static <V> LazyValue<V> from(final Function0<V> value) {
return new ImmutableLazyValue<V>() {
public V delegate() {
return get.apply();
}
};
}
}

public static abstract class CachedLazyValue<V> extends ImmutableLazyValue<V> {

public void invalidateCache() {
computed = false;
}

public static <V> CachedLazyValue<V> from(final Function0<V> get) {
return new CachedLazyValue<V>() {
public V delegate() {
return get.apply();
public V _apply() {
return value.apply();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
import java.util.HashMap;
import java.util.Map;

public abstract class Cached<K,V> {
import com.google.common.base.Function;

public abstract class Memoizing<K,V> implements Function<K,V> {

private final Map<K,V> cache = new HashMap<K,V>();

/**
* @param key must not be mutated.
*/
public abstract V delegate(K key);
protected abstract V _apply(K key);

public V get(K key) {
public final V apply(K key) {
if (cache.containsKey(key))
return cache.get(key);
V value = delegate(key);
V value = _apply(key);
if (value != null) {
cache.put(key, value);
return value; }
Expand Down

0 comments on commit e008364

Please sign in to comment.