Skip to content

Commit 8224eeb

Browse files
committed
Add AbstractHigherOrderDataHandle
Abstract superclass for DataHandles that operate over other DataHandles
1 parent e2a0b3f commit 8224eeb

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.io.handle;
34+
35+
import java.io.IOException;
36+
37+
import org.scijava.io.location.Location;
38+
39+
/**
40+
* Abstract superclass for {@link DataHandle}s that operate over other
41+
* {@link DataHandle}s.
42+
*
43+
* @author Gabriel Einsdorf
44+
*/
45+
public abstract class AbstractHigherOrderHandle<L extends Location> extends
46+
AbstractDataHandle<L>
47+
{
48+
49+
private final DataHandle<L> handle;
50+
private boolean closed;
51+
52+
public AbstractHigherOrderHandle(final DataHandle<L> handle) {
53+
this.handle = handle;
54+
set(handle.get()); // provides access to underlying location
55+
}
56+
57+
@Override
58+
public boolean isReadable() {
59+
return !closed && handle.isReadable();
60+
}
61+
62+
@Override
63+
public boolean isWritable() {
64+
return !closed && handle.isWritable();
65+
}
66+
67+
@Override
68+
public long length() throws IOException {
69+
return handle.length();
70+
}
71+
72+
@Override
73+
public Class<L> getType() {
74+
return handle.getType();
75+
}
76+
77+
@Override
78+
public boolean exists() throws IOException {
79+
return handle.exists();
80+
}
81+
82+
@Override
83+
public void close() throws IOException {
84+
if (!closed) {
85+
cleanup();
86+
closed = true;
87+
handle.close();
88+
}
89+
}
90+
91+
protected void ensureOpen() throws IOException {
92+
if (closed) {
93+
throw new IOException("This handle is closed!");
94+
}
95+
}
96+
97+
/**
98+
* Clean up data structures after a handle has been closed in the
99+
* {@link #close()} method.
100+
*
101+
* @throws IOException
102+
*/
103+
protected abstract void cleanup() throws IOException;
104+
105+
/**
106+
* @return the {@link DataHandle} wrapped by this
107+
* {@link AbstractHigherOrderHandle}
108+
*/
109+
protected DataHandle<L> handle() {
110+
return handle;
111+
}
112+
113+
}

0 commit comments

Comments
 (0)