| 1 | |
package org.webslinger.bsf; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
|
| 5 | |
import org.apache.bsf.BSFException; |
| 6 | |
|
| 7 | |
import org.webslinger.lang.Freezable; |
| 8 | |
|
| 9 | 0 | public abstract class Key<C> implements Freezable<Key<C>> { |
| 10 | |
protected final Object id; |
| 11 | 1471 | protected boolean hashCodeComputed = false; |
| 12 | |
protected int computedHashCode; |
| 13 | |
|
| 14 | 1467 | protected Key(Object id) { |
| 15 | 1468 | this.id = id; |
| 16 | 1467 | } |
| 17 | |
|
| 18 | |
public Object getId() { |
| 19 | 5441 | return id; |
| 20 | |
} |
| 21 | |
|
| 22 | |
public boolean equals(Object o) { |
| 23 | 1190 | if (!(o instanceof Key)) return false; |
| 24 | |
|
| 25 | 1190 | Key other = (Key) o; |
| 26 | 1195 | return keyEquals(other); |
| 27 | |
} |
| 28 | |
|
| 29 | |
public int hashCode() { |
| 30 | 1744 | if (hashCodeComputed) return computedHashCode; |
| 31 | 1469 | computedHashCode = computeHashCode(); |
| 32 | 1468 | hashCodeComputed = true; |
| 33 | 1471 | return computedHashCode; |
| 34 | |
} |
| 35 | |
|
| 36 | |
public Key<C> freeze() { |
| 37 | 0 | return this; |
| 38 | |
} |
| 39 | |
|
| 40 | |
protected int computeHashCode() { |
| 41 | 1471 | return getId().hashCode(); |
| 42 | |
} |
| 43 | |
|
| 44 | |
protected boolean keyEquals(Key key) { |
| 45 | 1194 | return getId().equals(key.getId()); |
| 46 | |
} |
| 47 | |
|
| 48 | |
public long getLastModifiedTime(GenericEngine engine) throws BSFException { |
| 49 | |
try { |
| 50 | 842 | return engine.getVFSDelegate().getLastModifiedTime(getId()); |
| 51 | 0 | } catch (IOException e) { |
| 52 | 0 | throw (BSFException) new BSFException(BSFException.REASON_IO_ERROR, e.getMessage()).initCause(e); |
| 53 | |
} |
| 54 | |
} |
| 55 | |
|
| 56 | |
public String getText(GenericEngine engine) throws BSFException { |
| 57 | |
try { |
| 58 | 281 | return engine.getVFSDelegate().getString(getId()); |
| 59 | 0 | } catch (IOException e) { |
| 60 | 0 | throw (BSFException) new BSFException(BSFException.REASON_IO_ERROR, e.getMessage()).initCause(e); |
| 61 | |
} |
| 62 | |
} |
| 63 | |
|
| 64 | |
public ClassLoader getParentClassLoader(GenericEngine engine) { |
| 65 | 260 | ClassLoader loader = engine.getManager().getClassLoader(); |
| 66 | 260 | return loader != null ? loader : Thread.currentThread().getContextClassLoader(); |
| 67 | |
} |
| 68 | |
|
| 69 | |
public abstract <V> C compile(Compiler<V, C> compiler, Compiler<V, C>.CompilerContext context) throws BSFException; |
| 70 | |
} |