Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tighter, more dangerous idiom
  • Loading branch information
ajs6f committed Jun 26, 2015
1 parent 0e52cc6 commit f2b6c34
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
Expand Up @@ -17,13 +17,12 @@

import static com.google.common.collect.Maps.filterEntries;
import static javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE;
import static org.fcrepo.kernel.utils.UncheckedBiConsumer.uncheck;
import static org.openrdf.model.impl.ValueFactoryImpl.getInstance;
import static org.openrdf.model.util.Literals.createLiteral;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.OutputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;

import javax.ws.rs.WebApplicationException;
Expand Down Expand Up @@ -119,11 +118,9 @@ private void write(final Iterable<Statement> model,
* - xmlns, which Sesame helpfully serializes, but normal parsers may complain
* about in some serializations (e.g. RDF/XML where xmlns:xmlns is forbidden by XML);
*/
final Map<String,String> namespaces = filterEntries(rdfStream.namespaces(), e -> !e.getKey().equals("xmlns"));
filterEntries(rdfStream.namespaces(), e -> !e.getKey().equals("xmlns"))
.forEach(uncheck(writer::handleNamespace));

for (final Entry<String, String> e : namespaces.entrySet()) {
writer.handleNamespace(e.getKey(), e.getValue());
}
Rio.write(model, writer);
}

Expand Down
@@ -0,0 +1,61 @@
/**
* Copyright 2015 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.kernel.utils;

import java.util.function.BiConsumer;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;

/**
* It is sometimes convenient to use an operation that throws checked exceptions inside of a functional incantation.
* This type permits that. It should be used with great caution! Only uncheck exceptions for which no reasonable
* recovery is available.
*
* @author ajs6f
* @param <S>
* @param <T>
*/
@FunctionalInterface
public interface UncheckedBiConsumer<S, T> extends BiConsumer<S, T> {

@Override
default void accept(final S first, final T second) {
try {
acceptThrows(first, second);
} catch (final Exception e) {
throw new RepositoryRuntimeException(e);
}
}

/**
* The same semantic as {@link #accept(Object, Object)}, but allowed to throw exceptions.
*
* @param first
* @param second
* @throws Exception
*/
void acceptThrows(final S first, final T second) throws Exception;

/**
* A convenience method to construct <code>UncheckedBiConsumers</code> from lambda syntax.
*
* @param c
* @return an UncheckedBiConsumer
*/
static <S, T> UncheckedBiConsumer<S, T> uncheck(final UncheckedBiConsumer<S, T> c) {
return c;
}
}

0 comments on commit f2b6c34

Please sign in to comment.