Skip to content

Commit 159f032

Browse files
committed
OAK-11571: commons: add Closer class (similar to Guava Closer) - fix line ends
1 parent 01179a5 commit 159f032

File tree

1 file changed

+126
-126
lines changed
  • oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/pio

1 file changed

+126
-126
lines changed
Lines changed: 126 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,126 @@
1-
/*
2-
* Licensed to the Apache Software Foundation (ASF) under one
3-
* or more contributor license agreements. See the NOTICE file
4-
* distributed with this work for additional information
5-
* regarding copyright ownership. The ASF licenses this file
6-
* to you under the Apache License, Version 2.0 (the
7-
* "License"); you may not use this file except in compliance
8-
* with the License. You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
12-
* Unless required by applicable law or agreed to in writing,
13-
* software distributed under the License is distributed on an
14-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15-
* KIND, either express or implied. See the License for the
16-
* specific language governing permissions and limitations
17-
* under the License.
18-
*/
19-
package org.apache.jackrabbit.oak.commons.pio;
20-
21-
import org.jetbrains.annotations.NotNull;
22-
import org.jetbrains.annotations.Nullable;
23-
24-
import java.io.Closeable;
25-
import java.io.IOException;
26-
import java.util.ArrayDeque;
27-
import java.util.Deque;
28-
import java.util.Objects;
29-
30-
/**
31-
* Convenience utility to close a list of {@link Closeable}s in reverse order,
32-
* suppressing all but the first exception to occur.
33-
* <p>
34-
* Inspired by and replacing Guava's Closer.
35-
*/
36-
public class Closer implements Closeable {
37-
38-
private Closer() {
39-
// no instances for you
40-
}
41-
42-
// stack of closeables to close, in general will be few
43-
private final Deque<Closeable> closeables = new ArrayDeque<>(3);
44-
45-
// flag set by rethrow method
46-
private boolean suppressExceptionsOnClose = false;
47-
48-
/**
49-
* Create instance of Closer.
50-
*/
51-
public static Closer create() {
52-
return new Closer();
53-
}
54-
55-
/**
56-
* Add a {@link Closeable} to the list.
57-
* @param closeable {@link Closeable} object to be added
58-
* @return the closeable param
59-
*/
60-
public @Nullable <C extends Closeable> C register(@Nullable C closeable) {
61-
if (closeable != null) {
62-
closeables.add(closeable);
63-
}
64-
return closeable;
65-
}
66-
67-
/**
68-
* Closes the set of {@link Closeable}s in reverse order.
69-
* <p>
70-
* Swallows all exceptions except the first that
71-
* was thrown.
72-
* <p>
73-
* If {@link #rethrow} was called before, even the first
74-
* exception will be suppressed.
75-
*/
76-
public void close() throws IOException {
77-
// keep track of the IOException to throw
78-
Throwable toThrow = null;
79-
80-
// close all in reverse order
81-
while (!closeables.isEmpty()) {
82-
Closeable closeable = closeables.removeLast();
83-
try {
84-
closeable.close();
85-
} catch (Throwable exception) {
86-
// remember the first one that occurred
87-
if (toThrow == null) {
88-
toThrow = exception;
89-
}
90-
}
91-
}
92-
93-
// exceptions are suppressed when retrow was called
94-
if (!suppressExceptionsOnClose && toThrow != null) {
95-
// due to the contract of Closeable, the exception is either
96-
// a checked IOException or an unchecked exception
97-
if (toThrow instanceof IOException) {
98-
throw (IOException) toThrow;
99-
} else {
100-
throw (RuntimeException) toThrow;
101-
}
102-
}
103-
}
104-
105-
/**
106-
* Sets a flag indicating that this method was called, then rethrows the
107-
* given exception (potentially wrapped into {@link Error} or {@link RuntimeException}).
108-
* <p>
109-
* {@link #close()} will not throw when this method was called before.
110-
* @return never returns
111-
* @throws IOException wrapping the input, when needed
112-
*/
113-
public RuntimeException rethrow(@NotNull Throwable throwable) throws IOException {
114-
Objects.requireNonNull(throwable);
115-
suppressExceptionsOnClose = true;
116-
if (throwable instanceof IOException) {
117-
throw (IOException) throwable;
118-
} else if (throwable instanceof RuntimeException) {
119-
throw (RuntimeException) throwable;
120-
} else if (throwable instanceof Error) {
121-
throw (Error) throwable;
122-
} else {
123-
throw new RuntimeException(throwable);
124-
}
125-
}
126-
}
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.jackrabbit.oak.commons.pio;
20+
21+
import org.jetbrains.annotations.NotNull;
22+
import org.jetbrains.annotations.Nullable;
23+
24+
import java.io.Closeable;
25+
import java.io.IOException;
26+
import java.util.ArrayDeque;
27+
import java.util.Deque;
28+
import java.util.Objects;
29+
30+
/**
31+
* Convenience utility to close a list of {@link Closeable}s in reverse order,
32+
* suppressing all but the first exception to occur.
33+
* <p>
34+
* Inspired by and replacing Guava's Closer.
35+
*/
36+
public class Closer implements Closeable {
37+
38+
private Closer() {
39+
// no instances for you
40+
}
41+
42+
// stack of closeables to close, in general will be few
43+
private final Deque<Closeable> closeables = new ArrayDeque<>(3);
44+
45+
// flag set by rethrow method
46+
private boolean suppressExceptionsOnClose = false;
47+
48+
/**
49+
* Create instance of Closer.
50+
*/
51+
public static Closer create() {
52+
return new Closer();
53+
}
54+
55+
/**
56+
* Add a {@link Closeable} to the list.
57+
* @param closeable {@link Closeable} object to be added
58+
* @return the closeable param
59+
*/
60+
public @Nullable <C extends Closeable> C register(@Nullable C closeable) {
61+
if (closeable != null) {
62+
closeables.add(closeable);
63+
}
64+
return closeable;
65+
}
66+
67+
/**
68+
* Closes the set of {@link Closeable}s in reverse order.
69+
* <p>
70+
* Swallows all exceptions except the first that
71+
* was thrown.
72+
* <p>
73+
* If {@link #rethrow} was called before, even the first
74+
* exception will be suppressed.
75+
*/
76+
public void close() throws IOException {
77+
// keep track of the IOException to throw
78+
Throwable toThrow = null;
79+
80+
// close all in reverse order
81+
while (!closeables.isEmpty()) {
82+
Closeable closeable = closeables.removeLast();
83+
try {
84+
closeable.close();
85+
} catch (Throwable exception) {
86+
// remember the first one that occurred
87+
if (toThrow == null) {
88+
toThrow = exception;
89+
}
90+
}
91+
}
92+
93+
// exceptions are suppressed when retrow was called
94+
if (!suppressExceptionsOnClose && toThrow != null) {
95+
// due to the contract of Closeable, the exception is either
96+
// a checked IOException or an unchecked exception
97+
if (toThrow instanceof IOException) {
98+
throw (IOException) toThrow;
99+
} else {
100+
throw (RuntimeException) toThrow;
101+
}
102+
}
103+
}
104+
105+
/**
106+
* Sets a flag indicating that this method was called, then rethrows the
107+
* given exception (potentially wrapped into {@link Error} or {@link RuntimeException}).
108+
* <p>
109+
* {@link #close()} will not throw when this method was called before.
110+
* @return never returns
111+
* @throws IOException wrapping the input, when needed
112+
*/
113+
public RuntimeException rethrow(@NotNull Throwable throwable) throws IOException {
114+
Objects.requireNonNull(throwable);
115+
suppressExceptionsOnClose = true;
116+
if (throwable instanceof IOException) {
117+
throw (IOException) throwable;
118+
} else if (throwable instanceof RuntimeException) {
119+
throw (RuntimeException) throwable;
120+
} else if (throwable instanceof Error) {
121+
throw (Error) throwable;
122+
} else {
123+
throw new RuntimeException(throwable);
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)