org.sitemap4j.reader.RawSiteMap.java Source code

Java tutorial

Introduction

Here is the source code for org.sitemap4j.reader.RawSiteMap.java

Source

/*
 * Copyright (C) 2012 Nicolas A. Brard-Nault
 *
 * 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.sitemap4j.reader;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.sitemap4j.value.AbstractValueObject;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Maps;

public class RawSiteMap extends AbstractValueObject {
    final private InputStream inputStream;
    final private Map<Object, Object> representations;

    private RawSiteMap(final InputStream inputstream) {
        checkNotNull(inputstream);
        this.inputStream = inputstream;
        inputstream.mark(0);

        this.representations = Maps.newConcurrentMap();
    }

    public <T> void addRepresentation(final Function<InputStream, T> function) {
        addRepresentation(function, Optional.<Class<T>>absent());
    }

    public <T> void addRepresentation(final Function<InputStream, T> function,
            final Optional<Class<T>> representationClass) {
        checkNotNull(function);

        try {
            if (!inputStream.markSupported() && representations.size() != 0) {
                throw new RuntimeException("InputStream does not support mark and stream was already read");
            }

            inputStream.reset();
            final T representation = function.apply(inputStream);

            if (!representationClass.isPresent()) {
                representations.put(representation.getClass(), representation);
            } else {
                representations.put(representationClass.get(), representation);
            }
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }

    public <T> boolean hasRepresentation(final Class<T> reprensentationClass) {
        checkNotNull(reprensentationClass);
        return representations.containsKey(reprensentationClass);
    }

    @SuppressWarnings("unchecked")
    public <T> Optional<T> getRepresentation(final Class<T> reprensentationClass) {
        checkNotNull(reprensentationClass);

        if (hasRepresentation(reprensentationClass)) {
            return Optional.of((T) representations.get(reprensentationClass));
        } else {
            return Optional.absent();
        }
    }

    public static RawSiteMap fromInputStream(final InputStream inputstream) {
        checkNotNull(inputstream);

        return new RawSiteMap(inputstream);
    }
}