text:
package org.apache.qpid.protonj2.codec.decoders;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.qpid.protonj2.buffer.ProtonBuffer;
import org.apache.qpid.protonj2.codec.DecodeException;
import org.apache.qpid.protonj2.codec.EncodingCodes;
import org.apache.qpid.protonj2.codec.StreamDecoder;
import org.apache.qpid.protonj2.codec.StreamDecoderState;
import org.apache.qpid.protonj2.codec.StreamDescribedTypeDecoder;
import org.apache.qpid.protonj2.codec.StreamTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Array32TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Array8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Binary32TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Binary8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.BooleanFalseTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.BooleanTrueTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.BooleanTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.ByteTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.CharacterTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Decimal128TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Decimal32TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Decimal64TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.DoubleTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.FloatTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Integer32TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Integer8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.List0TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.List32TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.List8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Long8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.LongTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Map32TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Map8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.NullTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.ShortTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.String32TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.String8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Symbol32TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.Symbol8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.TimestampTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.UUIDTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.UnsignedByteTypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.UnsignedInteger0TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.UnsignedInteger32TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.UnsignedInteger8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.UnsignedLong0TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.UnsignedLong64TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.UnsignedLong8TypeDecoder;
import org.apache.qpid.protonj2.codec.decoders.primitives.UnsignedShortTypeDecoder;
import org.apache.qpid.protonj2.types.Binary;
import org.apache.qpid.protonj2.types.Decimal128;
import org.apache.qpid.protonj2.types.Decimal32;
import org.apache.qpid.protonj2.types.Decimal64;
import org.apache.qpid.protonj2.types.DeliveryTag;
import org.apache.qpid.protonj2.types.Symbol;
import org.apache.qpid.protonj2.types.UnsignedByte;
import org.apache.qpid.protonj2.types.UnsignedInteger;
import org.apache.qpid.protonj2.types.UnsignedLong;
import org.apache.qpid.protonj2.types.UnsignedShort;

/**
 * The default AMQP Decoder implementation.
 */
public final class ProtonStreamDecoder implements StreamDecoder {

    private static final int STREAM_PEEK_MARK_LIMIT = 64;

    // The decoders for primitives are fixed and cannot be altered by users who want
    // to register custom decoders.  The decoders created here are stateless and can be
    // made static to reduce overhead of creating Decoder instances.
    private static final PrimitiveTypeDecoder<?>[] primitiveDecoders = new PrimitiveTypeDecoder[256];

    static {
        primitiveDecoders[EncodingCodes.BOOLEAN & 0xFF] = new BooleanTypeDecoder();
        primitiveDecoders[EncodingCodes.BOOLEAN_TRUE & 0xFF] = new BooleanTrueTypeDecoder();
        primitiveDecoders[EncodingCodes.BOOLEAN_FALSE & 0xFF] = new BooleanFalseTypeDecoder();
        primitiveDecoders[EncodingCodes.VBIN8 & 0xFF] = new Binary8TypeDecoder();
        primitiveDecoders[EncodingCodes.VBIN32 & 0xFF] = new Binary32TypeDecoder();
        primitiveDecoders[EncodingCodes.BYTE & 0xFF] = new ByteTypeDecoder();
        primitiveDecoders[EncodingCodes.CHAR & 0xFF] = new CharacterTypeDecoder();
        primitiveDecoders[EncodingCodes.DECIMAL32 & 0xFF] = new Decimal32TypeDecoder();
        primitiveDecoders[EncodingCodes.DECIMAL64 & 0xFF] = new Decimal64TypeDecoder();
        primitiveDecoders[EncodingCodes.DECIMAL128 & 0xFF] = new Decimal128TypeDecoder();
        primitiveDecoders[EncodingCodes.DOUBLE & 0xFF] = new DoubleTypeDecoder();
        primitiveDecoders[EncodingCodes.FLOAT & 0xFF] = new FloatTypeDecoder();
        primitiveDecoders[EncodingCodes.NULL & 0xFF] = new NullTypeDecoder();
        primitiveDecoders[EncodingCodes.SHORT & 0xFF] = new ShortTypeDecoder();
        primitiveDecoders[EncodingCodes.SMALLINT & 0xFF] = new Integer8TypeDecoder();
        primitiveDecoders[EncodingCodes.INT & 0xFF] = new Integer32TypeDecoder();
        primitiveDecoders[EncodingCodes.SMALLLONG & 0xFF] = new Long8TypeDecoder();
        primitiveDecoders[EncodingCodes.LONG & 0xFF] = new LongTypeDecoder();
        primitiveDecoders[EncodingCodes.UBYTE & 0xFF] = new UnsignedByteTypeDecoder();
        primitiveDecoders[EncodingCodes.USHORT & 0xFF] = new UnsignedShortTypeDecoder();
        primitiveDecoders[EncodingCodes.UINT0 & 0xFF] = new UnsignedInteger0TypeDecoder();
        primitiveDecoders[EncodingCodes.SMALLUINT & 0xFF] = new UnsignedInteger8TypeDecoder();
        primitiveDecoders[EncodingCodes.UINT & 0xFF] = new UnsignedInteger32TypeDecoder();
        primitiveDecoders[EncodingCodes.ULONG0 & 0xFF] = new UnsignedLong0TypeDecoder();
        primitiveDecoders[EncodingCodes.SMALLULONG & 0xFF] = new UnsignedLong8TypeDecoder();
        primitiveDecoders[EncodingCodes.ULONG & 0xFF] = new UnsignedLong64TypeDecoder();
        primitiveDecoders[EncodingCodes.STR8 & 0xFF] = new String8TypeDecoder();
        primitiveDecoders[EncodingCodes.STR32 & 0xFF] = new String32TypeDecoder();
        primitiveDecoders[EncodingCodes.SYM8 & 0xFF] = new Symbol8TypeDecoder();
        primitiveDecoders[EncodingCodes.SYM32 & 0xFF] = new Symbol32TypeDecoder();
        primitiveDecoders[EncodingCodes.UUID & 0xFF] = new UUIDTypeDecoder();
        primitiveDecoders[EncodingCodes.TIMESTAMP & 0xFF] = new TimestampTypeDecoder();
        primitiveDecoders[EncodingCodes.LIST0 & 0xFF] = new List0TypeDecoder();
        primitiveDecoders[EncodingCodes.LIST8 & 0xFF] = new List8TypeDecoder();
        primitiveDecoders[EncodingCodes.LIST32 & 0xFF] = new List32TypeDecoder();
        primitiveDecoders[EncodingCodes.MAP8 & 0xFF] = new Map8TypeDecoder();
        primitiveDecoders[EncodingCodes.MAP32 & 0xFF] = new Map32TypeDecoder();
        primitiveDecoders[EncodingCodes.ARRAY8 & 0xFF] = new Array8TypeDecoder();
        primitiveDecoders[EncodingCodes.ARRAY32 & 0xFF] = new Array32TypeDecoder();

        // Initialize the locally used primitive type decoders for the main API
        symbol8Decoder = (Symbol8TypeDecoder) primitiveDecoders[EncodingCodes.SYM8 & 0xFF];
        symbol32Decoder = (Symbol32TypeDecoder) primitiveDecoders[EncodingCodes.SYM32 & 0xFF];
        binary8Decoder = (Binary8TypeDecoder) primitiveDecoders[EncodingCodes.VBIN8 & 0xFF];
        binary32Decoder = (Binary32TypeDecoder) primitiveDecoders[EncodingCodes.VBIN32 & 0xFF];
        list8Decoder = (List8TypeDecoder) primitiveDecoders[EncodingCodes.LIST8 & 0xFF];
        list32Decoder = (List32TypeDecoder) primitiveDecoders[EncodingCodes.LIST32 & 0xFF];
        map8Decoder = (Map8TypeDecoder) primitiveDecoders[EncodingCodes.MAP8 & 0xFF];
        map32Decoder = (Map32TypeDecoder) primitiveDecoders[EncodingCodes.MAP32 & 0xFF];
        string32Decoder = (String32TypeDecoder) primitiveDecoders[EncodingCodes.STR32 & 0xFF];
        string8Decoder = (String8TypeDecoder) primitiveDecoders[EncodingCodes.STR8 & 0xFF];
    }

    // Registry of decoders for described types which can be updated with user defined
    // decoders as well as the default decoders.
    private Map<Object, StreamDescribedTypeDecoder<?>> describedTypeDecoders = new HashMap<>();

    // Quick access to decoders that handle AMQP types like Transfer, Properties etc.
    private final StreamDescribedTypeDecoder<?>[] amqpTypeDecoders = new StreamDescribedTypeDecoder[256];

    private ProtonStreamDecoderState singleThreadedState;

    // Internal Decoders used to prevent user to access Proton specific decoding methods
    private static final Symbol8TypeDecoder symbol8Decoder;
    private static final Symbol32TypeDecoder symbol32Decoder;
    private static final Binary8TypeDecoder binary8Decoder;
    private static final Binary32TypeDecoder binary32Decoder;
    private static final List8TypeDecoder list8Decoder;
    private static final List32TypeDecoder list32Decoder;
    private static final Map8TypeDecoder map8Decoder;
    private static final Map32TypeDecoder map32Decoder;
    private static final String8TypeDecoder string8Decoder;
    private static final String32TypeDecoder string32Decoder;

    @Override
    public ProtonStreamDecoderState newDecoderState() {
        return new ProtonStreamDecoderState(this);
    }

    @Override
    public ProtonStreamDecoderState getCachedDecoderState() {
        ProtonStreamDecoderState state = singleThreadedState;
        if (state == null) {
            singleThreadedState = state = newDecoderState();
        }

        return state.reset();
    }

    @Override
    public Object readObject(InputStream stream, StreamDecoderState state) throws DecodeException {
        StreamTypeDecoder<?> decoder = readNextTypeDecoder(stream, state);

        if (decoder == null) {
            throw new DecodeException("Unknown type constructor in encoded bytes");
        }

        return decoder.readValue(stream, state);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T readObject(InputStream stream, StreamDecoderState state, final Class<T> clazz) throws DecodeException {
        Object result = readObject(stream, state);

        if (result == null) {
            return null;
        } else if (clazz.isAssignableFrom(result.getClass())) {
            return (T) result;
        } else {
            throw signalUnexpectedType(result, clazz);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T[] readMultiple(InputStream stream, StreamDecoderState state, final Class<T> clazz) throws DecodeException {
        Object val = readObject(stream, state);

        if (val == null) {
            return null;
        } else if (val.getClass().isArray()) {
            if (clazz.isAssignableFrom(val.getClass().getComponentType())) {
                return (T[]) val;
            } else {
                throw signalUnexpectedType(val, Array.newInstance(clazz, 0).getClass());
            }
        } else if (clazz.isAssignableFrom(val.getClass())) {
            T[] array = (T[]) Array.newInstance(clazz, 1);
            array[0] = (T) val;
            return array;
        } else {
            throw signalUnexpectedType(val, Array.newInstance(clazz, 0).getClass());
        }
    }

    @Override
    public StreamTypeDecoder<?> readNextTypeDecoder(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        if (encodingCode == EncodingCodes.DESCRIBED_TYPE_INDICATOR) {
            if (stream.markSupported()) {
                stream.mark(STREAM_PEEK_MARK_LIMIT);
                try {
                    final long result = readUnsignedLong(stream, state, amqpTypeDecoders.length);

                    if (result > 0 && result < amqpTypeDecoders.length && amqpTypeDecoders[(int) result] != null) {
                        return amqpTypeDecoders[(int) result];
                    } else {
                        ProtonStreamUtils.reset(stream);
                        return slowReadNextTypeDecoder(stream, state);
                    }
                } catch (Exception e) {
                    ProtonStreamUtils.reset(stream);
                    return slowReadNextTypeDecoder(stream, state);
                }
            } else {
                return slowReadNextTypeDecoder(stream, state);
            }
        } else {
            return primitiveDecoders[encodingCode & 0xff];
        }
    }

    private StreamTypeDecoder<?> slowReadNextTypeDecoder(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);
        final Object descriptor;

        switch (encodingCode) {
            case EncodingCodes.SMALLULONG:
                descriptor = UnsignedLong.valueOf(ProtonStreamUtils.readByte(stream) & 0xffl);
                break;
            case EncodingCodes.ULONG:
                descriptor = UnsignedLong.valueOf(ProtonStreamUtils.readLong(stream));
                break;
            case EncodingCodes.SYM8:
                descriptor = symbol8Decoder.readValue(stream, state);
                break;
            case EncodingCodes.SYM32:
                descriptor = symbol32Decoder.readValue(stream, state);
                break;
            default:
                throw new DecodeException("Expected Descriptor type but found encoding: " + EncodingCodes.toString(encodingCode));
        }

        StreamTypeDecoder<?> StreamTypeDecoder = describedTypeDecoders.get(descriptor);
        if (StreamTypeDecoder == null) {
            StreamTypeDecoder = handleUnknownDescribedType(descriptor);
        }

        return StreamTypeDecoder;
    }

    @Override
    public StreamTypeDecoder<?> peekNextTypeDecoder(InputStream stream, StreamDecoderState state) throws DecodeException {
        if (stream.markSupported()) {
            stream.mark(STREAM_PEEK_MARK_LIMIT);
            try {
                return readNextTypeDecoder(stream, state);
            } finally {
                try {
                    stream.reset();
                } catch (IOException e) {
                    throw new DecodeException("Error while reseting marked stream", e);
                }
            }
        } else {
            throw new UnsupportedOperationException("The provided stream doesn't support stream marks");
        }
    }

    @Override
    public <V> ProtonStreamDecoder registerDescribedTypeDecoder(StreamDescribedTypeDecoder<V> decoder) {
        StreamDescribedTypeDecoder<?> describedTypeDecoder = decoder;

        // Cache AMQP type decoders in the quick lookup array.
        if (decoder.getDescriptorCode().compareTo(amqpTypeDecoders.length) < 0) {
            amqpTypeDecoders[decoder.getDescriptorCode().intValue()] = decoder;
        }

        describedTypeDecoders.put(describedTypeDecoder.getDescriptorCode(), describedTypeDecoder);
        describedTypeDecoders.put(describedTypeDecoder.getDescriptorSymbol(), describedTypeDecoder);

        return this;
    }

    @Override
    public Boolean readBoolean(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.BOOLEAN_TRUE:
                return Boolean.TRUE;
            case EncodingCodes.BOOLEAN_FALSE:
                return Boolean.FALSE;
            case EncodingCodes.BOOLEAN:
                return ProtonStreamUtils.readByte(stream) == 0 ? Boolean.FALSE : Boolean.TRUE;
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Boolean type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public boolean readBoolean(InputStream stream, StreamDecoderState state, boolean defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.BOOLEAN_TRUE:
                return true;
            case EncodingCodes.BOOLEAN_FALSE:
                return false;
            case EncodingCodes.BOOLEAN:
                return ProtonStreamUtils.readByte(stream) == 0 ? false : true;
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Boolean type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Byte readByte(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.BYTE:
                return ProtonStreamUtils.readByte(stream);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Byte type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public byte readByte(InputStream stream, StreamDecoderState state, byte defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.BYTE:
                return ProtonStreamUtils.readByte(stream);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Byte type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public UnsignedByte readUnsignedByte(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.UBYTE:
                return UnsignedByte.valueOf(ProtonStreamUtils.readByte(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Unsigned Byte type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public byte readUnsignedByte(InputStream stream, StreamDecoderState state, byte defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.UBYTE:
                return ProtonStreamUtils.readByte(stream);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Unsigned Byte type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Character readCharacter(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.CHAR:
                return Character.valueOf((char) (ProtonStreamUtils.readInt(stream) & 0xFFFF));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Character type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public char readCharacter(InputStream stream, StreamDecoderState state, char defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.CHAR:
                return (char) (ProtonStreamUtils.readInt(stream) & 0xFFFF);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Character type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Decimal32 readDecimal32(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.DECIMAL32:
                return new Decimal32(ProtonStreamUtils.readInt(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Decimal32 type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Decimal64 readDecimal64(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.DECIMAL64:
                return new Decimal64(ProtonStreamUtils.readLong(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Decimal64 type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Decimal128 readDecimal128(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.DECIMAL128:
                return new Decimal128(ProtonStreamUtils.readLong(stream), ProtonStreamUtils.readLong(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Decimal128 type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Short readShort(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.SHORT:
                return ProtonStreamUtils.readShort(stream);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Short type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public short readShort(InputStream stream, StreamDecoderState state, short defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.SHORT:
                return ProtonStreamUtils.readShort(stream);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Short type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public UnsignedShort readUnsignedShort(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.USHORT:
                return UnsignedShort.valueOf(ProtonStreamUtils.readShort(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Unsigned Short type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public short readUnsignedShort(InputStream stream, StreamDecoderState state, short defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.USHORT:
                return ProtonStreamUtils.readShort(stream);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Unsigned Short type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public int readUnsignedShort(InputStream stream, StreamDecoderState state, int defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.USHORT:
                return ProtonStreamUtils.readShort(stream) & 0xFFFF;
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Unsigned Short type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Integer readInteger(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.SMALLINT:
                return (int) ProtonStreamUtils.readByte(stream);
            case EncodingCodes.INT:
                return ProtonStreamUtils.readInt(stream);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Integer type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public int readInteger(InputStream stream, StreamDecoderState state, int defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.SMALLINT:
                return ProtonStreamUtils.readByte(stream);
            case EncodingCodes.INT:
                return ProtonStreamUtils.readInt(stream);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Integer type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public UnsignedInteger readUnsignedInteger(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.UINT0:
                return UnsignedInteger.ZERO;
            case EncodingCodes.SMALLUINT:
                return UnsignedInteger.valueOf(ProtonStreamUtils.readByte(stream) & 0xff);
            case EncodingCodes.UINT:
                return UnsignedInteger.valueOf(ProtonStreamUtils.readInt(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Unsigned Integer type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public int readUnsignedInteger(InputStream stream, StreamDecoderState state, int defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.UINT0:
                return 0;
            case EncodingCodes.SMALLUINT:
                return ProtonStreamUtils.readByte(stream) & 0xff;
            case EncodingCodes.UINT:
                return ProtonStreamUtils.readInt(stream);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Unsigned Integer type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public long readUnsignedInteger(InputStream stream, StreamDecoderState state, long defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.UINT0:
                return 0;
            case EncodingCodes.SMALLUINT:
                return ProtonStreamUtils.readByte(stream) & 0xffl;
            case EncodingCodes.UINT:
                return ProtonStreamUtils.readInt(stream) & 0xffffffffl;
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Unsigned Integer type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Long readLong(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.SMALLLONG:
                return Long.valueOf(ProtonStreamUtils.readByte(stream) & 0xffl);
            case EncodingCodes.LONG:
                return ProtonStreamUtils.readLong(stream);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Long type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public long readLong(InputStream stream, StreamDecoderState state, long defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.SMALLLONG:
                return ProtonStreamUtils.readByte(stream) & 0xffl;
            case EncodingCodes.LONG:
                return ProtonStreamUtils.readLong(stream);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Unsigned Long type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public UnsignedLong readUnsignedLong(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.ULONG0:
                return UnsignedLong.ZERO;
            case EncodingCodes.SMALLULONG:
                return UnsignedLong.valueOf(ProtonStreamUtils.readByte(stream) & 0xffl);
            case EncodingCodes.ULONG:
                return UnsignedLong.valueOf(ProtonStreamUtils.readLong(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Unsigned Long type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public long readUnsignedLong(InputStream stream, StreamDecoderState state, long defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.ULONG0:
                return 0l;
            case EncodingCodes.SMALLULONG:
                return ProtonStreamUtils.readByte(stream) & 0xffl;
            case EncodingCodes.ULONG:
                return ProtonStreamUtils.readLong(stream);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Unsigned Long type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Float readFloat(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.FLOAT:
                return Float.intBitsToFloat(ProtonStreamUtils.readInt(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Float type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public float readFloat(InputStream stream, StreamDecoderState state, float defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.FLOAT:
                return Float.intBitsToFloat(ProtonStreamUtils.readInt(stream));
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Float type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Double readDouble(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.DOUBLE:
                return Double.longBitsToDouble(ProtonStreamUtils.readLong(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Double type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public double readDouble(InputStream stream, StreamDecoderState state, double defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.DOUBLE:
                return Double.longBitsToDouble(ProtonStreamUtils.readLong(stream));
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Double type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Binary readBinary(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.VBIN8:
                return binary8Decoder.readValue(stream, state);
            case EncodingCodes.VBIN32:
                return binary32Decoder.readValue(stream, state);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Binary type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public ProtonBuffer readBinaryAsBuffer(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.VBIN8:
                return binary8Decoder.readValueAsBuffer(stream, state);
            case EncodingCodes.VBIN32:
                return binary32Decoder.readValueAsBuffer(stream, state);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Binary type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public DeliveryTag readDeliveryTag(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.VBIN8:
                return new DeliveryTag.ProtonDeliveryTag(binary8Decoder.readValueAsArray(stream, state));
            case EncodingCodes.VBIN32:
                return new DeliveryTag.ProtonDeliveryTag(binary32Decoder.readValueAsArray(stream, state));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Binary type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public String readString(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.STR8:
                return string8Decoder.readValue(stream, state);
            case EncodingCodes.STR32:
                return string32Decoder.readValue(stream, state);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected String type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Symbol readSymbol(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.SYM8:
                return symbol8Decoder.readValue(stream, state);
            case EncodingCodes.SYM32:
                return symbol32Decoder.readValue(stream, state);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Symbol type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public String readSymbol(InputStream stream, StreamDecoderState state, String defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.SYM8:
                return symbol8Decoder.readString(stream, state);
            case EncodingCodes.SYM32:
                return symbol32Decoder.readString(stream, state);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Symbol type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public Long readTimestamp(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.TIMESTAMP:
                return ProtonStreamUtils.readLong(stream);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Timestamp type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public long readTimestamp(InputStream stream, StreamDecoderState state, long defaultValue) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.TIMESTAMP:
                return ProtonStreamUtils.readLong(stream);
            case EncodingCodes.NULL:
                return defaultValue;
            default:
                throw new DecodeException("Expected Timestamp type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @Override
    public UUID readUUID(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.UUID:
                return new UUID(ProtonStreamUtils.readLong(stream), ProtonStreamUtils.readLong(stream));
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected UUID type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public <K, V> Map<K, V> readMap(InputStream stream, StreamDecoderState state) throws DecodeException {
         final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.MAP8:
                return (Map<K, V>) map8Decoder.readValue(stream, state);
            case EncodingCodes.MAP32:
                return (Map<K, V>) map32Decoder.readValue(stream, state);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected Map type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public <V> List<V> readList(InputStream stream, StreamDecoderState state) throws DecodeException {
        final byte encodingCode = ProtonStreamUtils.readEncodingCode(stream);

        switch (encodingCode) {
            case EncodingCodes.LIST0:
                return Collections.emptyList();
            case EncodingCodes.LIST8:
                return (List<V>) list8Decoder.readValue(stream, state);
            case EncodingCodes.LIST32:
                return (List<V>) list32Decoder.readValue(stream, state);
            case EncodingCodes.NULL:
                return null;
            default:
                throw new DecodeException("Expected List type but found encoding: " + EncodingCodes.toString(encodingCode));
        }
    }

    private ClassCastException signalUnexpectedType(final Object val, Class<?> clazz) {
        return new ClassCastException("Unexpected type " + val.getClass().getName() +
                                      ". Expected " + clazz.getName() + ".");
    }

    private StreamTypeDecoder<?> handleUnknownDescribedType(final Object descriptor) {
        StreamTypeDecoder<?> StreamTypeDecoder = new UnknownDescribedTypeDecoder() {

            @Override
            public Object getDescriptor() {
                return descriptor;
            }
        };

        describedTypeDecoders.put(descriptor, (UnknownDescribedTypeDecoder) StreamTypeDecoder);

        return StreamTypeDecoder;
    }
}


text:
Theory
======

The RT1 module provides a method for calculating the scattered radiation from a
uniformly illuminated rough surface covered by a homogeneous layer of tenuous media.
The following sections are intended to give a general overview of the underlying theory of the
RT1 module. A more general discussion on the derivation of the used results can be found in [QuWa16]_.
Details on how to define the scattering properties of the covering layer and the ground surface
within the RT1-module are given in :ref:`cha_model_specification`.

The utilized theoretical framework is based on applying the Radiative Transfer Equation (RTE) :eq:`RTE` to
the geometry shown in :numref:`problem_geometry`.

.. math::
   \cos(\theta) \frac{\partial I_f(r,\theta,\phi)}{\partial r} = -\kappa_{ex} I_f(r,\theta,\phi)
   + \kappa_s \int\limits_{0}^{2\pi}\int\limits_{0}^{\pi/2} I_f(r,\theta',\phi') \hat{p}(\theta,\phi,\theta',\phi') \sin(\theta') d\theta' d\phi'
   :label: RTE

The individual variables are hereby defined as follows:

- :math:`\theta` denotes the polar angle in a spherical coordinate system
- :math:`\phi` denotes the azimuth angle in a spherical coordinate system
- :math:`r` denotes the distance within the covering layer
- :math:`I_f(r,\theta,\phi)` denotes the specific intensity at a distance :math:`r` within the covering layer propagating in direction :math:`(\theta,\phi)`.
- :math:`\kappa_{ex}` denotes the extinction-coefficient (i.e. extinction cross section per unit volume)
- :math:`\hat{p}(\theta,\phi,\theta',\phi')` denotes the scattering phase-function of the constituents of the covering layer

.. note::
	To remain consistent with [QuWa16]_, the arguments of the functions :math:`\hat{p}(\theta,\phi,\theta',\phi')` and :math:`BRDF(\theta,\phi,\theta',\phi')` are defined as angles
	with respect to a spherical coordinate system in the following discussion. Within the RT1-module however, the functions are defined with respect to the associated zenith-angles!

	A relationship between the module-functions and the functions within the subsequent discussion is therefore given by:

	:code:`SRF.brdf(t_0,t_ex,p_0,p_ex)` :math:`\hat{=} ~BRDF(\pi - \theta_0, \phi_0, \theta_{ex},\phi_{ex}) \quad` and :math:`\mbox{}\quad` :code:`V.p(t_0,t_ex,p_0,p_ex)` :math:`\hat{=} ~\hat{p}(\pi - \theta_0, \phi_0, \theta_{ex},\phi_{ex})`

Problem Geometry and Boundary Conditions
-----------------------------------------

.. _problem_geometry:

.. figure:: _static/problem_geometry.png
   :align: center
   :width: 60%
   :alt: geometry applied to the RT1 module
   :figwidth: 100%

   Illustration of the chosen geometry within the RT1-module (adapted from [QuWa16]_)

As shown in :numref:`problem_geometry`, the considered problem geometry is defined as a rough surface covered by a homogeneous layer of a scattering and absorbing medium.

In order to be able to solve the RTE :eq:`RTE`, the boundary-conditions are specified as follows:

- The top of the covering layer is uniformly illuminated at a single incidence-direction:

.. math::
      I_0(z=0,\theta,\phi) = \frac{I_0}{\sin(\theta)}	\delta(\theta - \theta_i) \delta(\phi - \phi_i)

- Radiation impinging at the ground surface is reflected upwards according to its associated Bidirectional Reflectance Distribution Function (BRDF)

.. math::
   I^+(z=-d, \theta, \phi) = \int_{0}^{2\pi} \int_{0}^{\pi} I^-(z=-d, \theta, \phi) BRDF(\theta,\phi,\theta',\phi') \sin(\theta') d\theta' d\phi'

The superscripts :math:`I^\pm` hereby indicate a separation between upwelling :math:`(+)` and downwelling :math:`(-)` intensity.

The additional specifications of the covering layer and the ground surface are summarized as follows:

Parameters used to describe the scattering properties of the covering layer
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

**Scattering Phase Function:**
(i.e. *normalized differential scattering cross section*)

.. math::
   \hat{p}(\theta,\phi,\theta',\phi') \qquad \textrm{with} \qquad   \int\limits_0^{2\pi} ~ \int\limits_{0}^{\pi} \hat{p}(\theta,\phi,\theta',\phi') \sin(\theta') d\theta' d\phi' = 1

**Optical Depth:**

.. math::
   \tau = \kappa_{ex} ~ d = (\kappa_{s} + \kappa_{a}) ~ d

where :math:`\kappa_{ex}` is the *extinction coefficient* (i.e. extinction cross section per unit volume)
, :math:`\kappa_{s}` is the *scattering coefficient* (i.e. scattering cross section per unit volume)
, :math:`\kappa_{a}` is the *absorption coefficient* (i.e. absorption cross section per unit volume)
and :math:`d` is the *total height of the covering layer*.

**Single Scattering Albedo:**

.. math::
   \omega = \frac{\kappa_{s}}{\kappa_{ex}} = \frac{\kappa_{s}}{\kappa_{s} + \kappa_{a}}   \leq 1

Parameters used to describe the scattering properties of the ground surface
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

**Bidirectional Reflectance Distribution Function:**

.. math::
   BRDF(\theta,\phi,\theta',\phi')  \qquad \textrm{with} \qquad   \int\limits_0^{2\pi} ~ \int\limits_{0}^{\pi/2} BRDF(\theta,\phi,\theta',\phi') \cos(\theta') \sin(\theta') d\theta' d\phi' = R(\theta,\phi) \leq 1

where :math:`R(\theta,\phi)` denotes the **Directional-Hemispherical Reflectance** of the ground surface.

TBD: perhaps describe also normalization conditions for p and BRDF

First-order solution to the RTE
--------------------------------

Incorporating the above specifications, a solution to the RTE is obtained by assuming that the scattering coefficient :math:`\kappa_s` of the covering layer is small (i.e. :math:`\kappa_s\ll 1`).
Using this assumption, the RTE is expanded into a series with respect to powers of :math:`\kappa_s`, given by:

.. math::
   I^+ = I_{\textrm{surface}} + I_{\textrm{volume}} + I_{\textrm{interaction}} + (I_{svs}) + \mathcal{O}(\kappa_s^2)

where the individual terms (representing the contributions to the scattered intensity at the top of the covering layer)  can be interpreted as follows:

- :math:`I_{\textrm{surface}}`:  radiation scattered once by the ground surface
- :math:`I_{\textrm{volume}}`: radiation scattered once within the covering layer
- :math:`I_{\textrm{interaction}}`: radiation scattered once by the ground surface and once within the covering layer
- :math:`I_{svs}`: radiation scattered twice by the ground surface and once within the covering layer
      (This contribution is assumed to be negligible due to the occurrence of second order surface-scattering)

After some algebraic manipulations the individual contributions are found to be given by (details can be found in [QuWa16]_):

.. math::
   I_{\textrm{surface}}(\theta_0, \phi_0, \theta_{ex}, \phi_{ex}) = I_0 e^{-\frac{\tau}{\cos(\theta_0)}} ~ e^{-\frac{\tau}{\cos(\theta_{ex})}} \cos(\theta_0) BRDF(\pi-\theta_0, \phi_0, \theta_{ex}, \phi_{ex})
   :label: surface_contribution

.. math::
   I_{\textrm{volume}}(\theta_0, \phi_0, \theta_{ex}, \phi_{ex}) = I_0 ~\omega ~ \frac{\cos(\theta_0)}{\cos(\theta_0) + \cos(\theta_{ex})} \left( 1 - e^{-\frac{\tau}{\cos(\theta_0)}} ~ e^{-\frac{\tau}{\cos(\theta_{ex})}}  \right)    \hat{p}(\pi-\theta_0, \phi_0, \theta_{ex}, \phi_{ex})
   :label: volume_contribution

.. math::
   I_{\textrm{interaction}}(\theta_0, \phi_0, \theta_{ex}, \phi_{ex}) = I_0 ~ \cos(\theta_0) ~ \omega \left\lbrace e^{-\frac{\tau}{\cos(\theta_{ex})}} F_{int}(\theta_0,\theta_{ex}) + e^{-\frac{\tau}{\cos(\theta_{ex})}} F_{int}(\theta_{ex},\theta_{0})    \right\rbrace
   :label: interaction_contribution

.. math::
   \textrm{with} \qquad \qquad F_{int}(\theta_0, \phi_0, \theta_{ex}, \phi_{ex}) = \int\limits_0^{2\pi} \int\limits_0^\pi   \frac{\cos(\theta)}{\cos(\theta_0)-\cos(\theta)} \left( e^{-\frac{\tau}{\cos(\theta_0)}} - e^{-\frac{\tau}{\cos(\theta)}} \right) ~ \hat{p}(\theta_0, \phi_0, \theta , \phi) BRDF(\pi - \theta, \phi, \theta_{ex}, \phi_{ex})  \sin(\theta) d\theta d\phi
   :label: F_int

Evaluation of the interaction-contribution
-------------------------------------------

In order to analytically evaluate the remaining integral appearing in the interaction-term, the BRDF and the scattering phase-function of the covering layer are approximated via a
Legendre-series in a (possibly generalized) scattering angle of the form:

.. math::
   BRDF(\theta, \phi, \theta_{s}, \phi_{s}) = \sum_{n=0}^{N_b} b_n P_n(\cos(\Theta_{a_b}))
   :label: brdf_expansion

.. math::
   \hat{p}(\theta, \phi, \theta_{s}, \phi_{s}) = \sum_{n=0}^{N_p} p_n P_n(\cos(\Theta_{a_p}))
   :label: p_expansion

where :math:`P_n(x)` denotes the :math:`\textrm{n}^\textrm{th}` Legendre-polynomial and the generalized scattering angle :math:`\Theta_a` is defined via:

.. math::
   \cos(\Theta_a) = a_0 \cos(\theta) \cos(\theta_{s}) + \sin(\theta) \sin(\theta_{s}) \left[a_1 \cos(\phi) \cos(\phi_{s}) + a_2 \sin(\phi) \sin(\phi_{s}) \right]
   :label: general_scat_angle

where :math:`\theta ,\phi` are the polar- and azimuth-angles of the incident radiation, :math:`\theta_{s}, \phi_{s}` are the polar- and azimuth-angles of the scattered radiation and :math:`a_1,a_2` and :math:`a_3`
are constants that allow consideration of off-specular and anisotropic effects within the approximations.

Once the :math:`b_n` and :math:`p_n` coefficients are known, the method developed in [QuWa16]_ is used to analytically solve :math:`I_{\textrm{interaction}}`.

This is done in two steps:

First, the so-called fn-coefficients are evaluated which are defined via:

.. math::
   \int_{0}^{2\pi} \hat{p}(\theta_0,\phi_0,\theta,\phi)BRDF(\pi - \theta, \phi, \theta_{ex},\phi_{ex}) d\phi  = \sum_{n=0}^{N_b + N_p} f_n(\theta_0,\phi_0,\theta_{ex},\phi_{ex}) \cos(\theta)^n
   :label: fn_coef_definition

Second, :math:`I_{\textrm{interaction}}` is evaluated using the analytic solution to the remaining :math:`\theta`-integral for a given set of fn-coefficients as presented in [QuWa16]_.

.. topic:: Example

	In the following, a simple example on how to evaluate the fn-coefficients is given.
	The ground is hereby defined as a Lambertian-surface and the covering layer is assumed to consist of Rayleigh-particles. Thus, we have: (:math:`R_0` hereby denotes the diffuse albedo of the surface)

	- :math:`BRDF(\theta, \phi, \theta_{ex},\phi_{ex}) = \frac{R_0}{\pi}`
	- :math:`p(\theta, \phi, \theta_{ex},\phi_{ex}) = \frac{3}{16\pi} (1+\cos(\Theta)^2) \quad` with :math:`\mbox{}\quad` :math:`\cos(\Theta) = \cos(\theta)\cos(\theta_{ex}) + \sin(\theta)\sin(\theta_{ex})\cos(\phi - \phi_{ex})`

	.. math::
	   INT &= \int_0^{2\pi} p(\theta_0, \phi_0, \theta,\phi) * BRDF(\pi-\theta, \phi, \theta_{ex},\phi_{ex}) d\phi
	   \\ &= \frac{3 R_0}{16 \pi^2} \int\limits_{0}^{2\pi}  (1+[\cos(\theta_0)\cos(\theta) + \sin(\theta_0)\sin(\theta)\cos(\phi_0 - \phi)]^2) d\phi
	   \\ &= \frac{3 R_0}{16 \pi^2} \int\limits_0^{2\pi} (1+ \mu_0^2 \mu^2 + 2 \mu_0 \mu \sin(\theta_0) \sin(\theta) \cos(\phi_0 - \phi) + (1-\mu_0)^2(1-\mu)^2 \cos(\phi_0 - \phi)^2 d\phi

	where the shorthand-notation :math:`\mu_x = \cos(\theta_x)` has been introduced.

	The above integral can now easily be solved by noticing:

	.. math::
	   \int\limits_0^{2\pi} \cos(\phi_0 - \phi)^n d\phi = \left\lbrace \begin{matrix} 2 \pi & \textrm{for } n=0 \\ 0 & \textrm{for } n=1 \\ \pi  & \textrm{for } n=2 \end{matrix} \right.

	Using some algebraic manipulations we therefore find:

	.. math::
	   INT = \frac{3 R_0}{16\pi} \Big[ (3-\mu_0^2) + (3 \mu_0 -1) \mu^2 \Big] = \sum_{n=0}^2 f_n ~ \mu^n
	   \\ \\
	   \Rightarrow \quad f_0 = \frac{3 R_0}{16\pi}(3-\mu_0^2) \qquad f_1 = 0 \qquad f_2 = \frac{3 R_0}{16\pi}(3 \mu_0 -1) \qquad f_n = 0 ~ \forall ~n>2

     An IPython-notebook that uses the RT1-module to evaluate the above fn-coefficients can be found `HERE <https://github.com/pygeo/rt1/tree/master/doc/examples/example_fn.ipynb>`_

.. rubric:: References
.. [QuWa16]  R.Quast and W.Wagner, "Analytical solution for first-order scattering in bistatic radiative transfer interaction problems of layered media," Appl.Opt.55, 5379-5386 (2016)

redpajama_set_name: RedPajamaGithub


text:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FontAssetGlobalTester : MonoBehaviour {

	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {

	}
}


text:
object Test extends App {
  assert(Option(42) contains 42)
  assert(Some(42) contains 42)
  assert(Option(BigInt(42)) contains 42)
  assert(Option(42) contains BigInt(42))
  assert(!(None contains 42))
  assert(Some(null) contains null)
  assert(!(Option(null) contains null))
}


text:
namespace WebServer.Server.Enums
{
    public enum HttpRequestMethod
    {
        GET, POST
    }
}


text:
import unittest

from reactivex import operators as ops
from reactivex.testing import ReactiveTest, TestScheduler

on_next = ReactiveTest.on_next
on_completed = ReactiveTest.on_completed
on_error = ReactiveTest.on_error
subscribe = ReactiveTest.subscribe
subscribed = ReactiveTest.subscribed
disposed = ReactiveTest.disposed
created = ReactiveTest.created

class TestTakeWithTime(unittest.TestCase):
    def test_take_zero(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(210, 1), on_next(220, 2), on_completed(230)
        )

        def create():
            return xs.pipe(ops.take_with_time(0))

        res = scheduler.start(create)

        assert res.messages == [on_completed(200)]
        assert xs.subscriptions == [subscribe(200, 200)]

    def test_take_some(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(210, 1), on_next(220, 2), on_next(230, 3), on_completed(240)
        )

        def create():
            return xs.pipe(ops.take_with_time(25))

        res = scheduler.start(create)

        assert res.messages == [on_next(210, 1), on_next(220, 2), on_completed(225)]
        assert xs.subscriptions == [subscribe(200, 225)]

    def test_take_late(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(210, 1), on_next(220, 2), on_completed(230)
        )

        def create():
            return xs.pipe(ops.take_with_time(50))

        res = scheduler.start(create)

        assert res.messages == [on_next(210, 1), on_next(220, 2), on_completed(230)]
        assert xs.subscriptions == [subscribe(200, 230)]

    def test_take_Error(self):
        scheduler = TestScheduler()
        ex = "ex"
        xs = scheduler.create_hot_observable(on_error(210, ex))

        def create():
            return xs.pipe(ops.take_with_time(50))

        res = scheduler.start(create)

        assert res.messages == [on_error(210, ex)]
        assert xs.subscriptions == [subscribe(200, 210)]

    def test_take_never(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable()

        def create():
            return xs.pipe(ops.take_with_time(50))

        res = scheduler.start(create)

        assert res.messages == [on_completed(250)]
        assert xs.subscriptions == [subscribe(200, 250)]

    def test_take_twice1(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(210, 1),
            on_next(220, 2),
            on_next(230, 3),
            on_next(240, 4),
            on_next(250, 5),
            on_next(260, 6),
            on_completed(270),
        )

        def create():
            return xs.pipe(
                ops.take_with_time(55),
                ops.take_with_time(35),
            )

        res = scheduler.start(create)

        assert res.messages == [
            on_next(210, 1),
            on_next(220, 2),
            on_next(230, 3),
            on_completed(235),
        ]
        assert xs.subscriptions == [subscribe(200, 235)]

    def test_take_twice2(self):
        scheduler = TestScheduler()
        xs = scheduler.create_hot_observable(
            on_next(210, 1),
            on_next(220, 2),
            on_next(230, 3),
            on_next(240, 4),
            on_next(250, 5),
            on_next(260, 6),
            on_completed(270),
        )

        def create():
            return xs.pipe(
                ops.take_with_time(35),
                ops.take_with_time(55),
            )

        res = scheduler.start(create)

        assert res.messages == [
            on_next(210, 1),
            on_next(220, 2),
            on_next(230, 3),
            on_completed(235),
        ]
        assert xs.subscriptions == [subscribe(200, 235)]


text:
<!DOCTYPE html>

    <meta charset="utf-8" />
    <META NAME="keywords" CONTENT="a-machine, hybrid publishing consortium, publishing, innovation, museums libraries, archives, digital, workflows, research, design, Moos Verlag, Merve Verlag, Photomuseum Winterthur, McLuhan Salon Candaian Embassy, Frankfurt Book Fair">
    <META NAME="description" CONTENT="The Consortium develops Open Source software for public infrastructures for publishing, covering key parts of the publishing workflow–lowering cost, speeding up production cycles and introducing new forms of publishing.">
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>

    <META NAME="DC.title" CONTENT="Empowering Publishers to Innovate">
    <META NAME="DC.creator" CONTENT="Simon Worthington, Loraine Furter">
    <META NAME="DC.subject" CONTENT="publishing, post-digital scholar, innovation">
    <META NAME="DC.description.abstract" CONTENT="The Consortium develops Open Source software for public infrastructures for publishing, covering key parts of the publishing workflow–lowering cost, speeding up production cycles and introducing new forms of publishing.">
    <META NAME="DC.date" CONTENT="2014-11-13">
    <META NAME="DC.type" CONTENT="Presentation">
    <META NAME="DC.format" CONTENT="text/html">
    <META NAME="DC.language" CONTENT="en">
    <META NAME="DC.identifier.URL" CONTENT="http://consortium.io">
    <META NAME="DC.rights" CONTENT="http://www.consortium.io/copyright">
    <META NAME="DC.identifier" CONTENT="">
    <META NAME="DC.contributor" CONTENT="HPC team">
    <META NAME="DC.publisher" CONTENT="Hybrid Publishing Consortium">
    <META NAME="DC.bibliographicCitation" CONTENT="">
    <LINK REL="SCHEMA.DC" HREF="http://purl.org/dc/elements/1.1/">

    <link rel="stylesheet" type="text/css" href="../../css/all.min.css"/>
    <link rel="stylesheet" type="text/css" href="../../fonts.css"/>

    <title>Empowering Publishers to Innovate</title>

    <link rel="shortcut icon" href="../../favicon.ico" />

BACK

Empowering Publishers to Innovate

INFO

By:

Simon Worthington

Date:

13.11.2014

Presentation - Simon Worthington, Post Digital Scholar Conference, Lüneburg, 13 November 2014

CONTENTS

Empowering Publishers to Innovate ↳

Empowering Publishers to Innovate

The Consortium

The problems we are addressing

Slides

KEYWORDS

publishing, innovation, museums libraries, archives, digital, workflows, research, design, Moos Verlag, Merve Verlag, Photomuseum Winterthur, McLuhan Salon Candaian Embassy, Frankfurt Book Fair

TOP

PRINT/PDF

Empowering Publishers to Innovate

Empowering Publishers to Innovate

Presentation - Simon Worthington, Post Digital Scholar Conference, Lüneburg 2014

The Consortium

The Consortium develops Open Source software for public infrastructures for publishing, covering key parts of the publishing workflow–lowering cost, speeding up production cycles and introducing new forms of publishing. We support software producers to innovate existing Open Source platforms. We also want to innovate the digital book and reading experience. Now that there are floods of Open Access content for the user to sort through, the book must evolve to take on fresh interface design challenges, as the tech industry is only delivering a poor imitation of the paper book. Core to what we do is open research–methods and design research processes–which we see as being as important as the software we produce.

The problems we are addressing

Our aim is to address the problems of small cultural publishers, across; art, design, museums, archives and Digital Humanities.

What are these problems:

They cannot access publics on mobile and tablets. Especially in the MLA sector where an endless stream of great museum publication stay in limited print runs with poor distribution.

Lastly publishers cannot afford to innovate, and if they do it's hard to scale, or distribute the technology.

The Press

At the core of the Consortium we have made a Open Source digital press, called TypeSetr, that accepts DOCX, HTML, Gdoc as inputs and automatically applies templated design layouts for EPUB, responsive HTML, PDF, POD and more. The press is 'single source' based, meaning if you make edits to one document, they can be updated across all formats. The press generates a structured document, which is machine readable. This is firstly for automated multi-format layout designs and second for Dynamic Publishing applying algorithmic and other document reuse.

A-machine

A-machine is our phase II software project. A-machine uses the structured document from the digital press to allow further semi-automation of the workflow known as Dynamic Publishing, this covers things like–distribution, translation management, payment, citation extraction etc. A-machine is an ecosystem of software, connecting existing Open Source platforms, such as–Zotero, Transpect from the company le-tex and Tamboti from the Heidelberg Research Architecture to support a wide variety of workflows.

Book to the Future

Our focus in on the near horizon of 'Post-Open Access Publishing', and how to enhance the book form in an open IPR context. Our lens on publishing is the publication lifecycle and workflow, and by studying these we gain insights to drive innovation. An example 'future book' scenario is to have all references available inline, and in full. The history of the future book can be framed as the 'Unbound Book', exemplified by the work of technologists like Alan Kay of Dynabook, Ted Nelson inventor of Xanadu, as well as in publishing histories like Markus Krajewski's book, Paper Machines.

Publishing Prototypes

The Hybrid Publishing Consortium has four publication prototypes;

First is with Fotomuseum Winterthur, and the publication Manifeste! Where we examine how the high quality museum catalog can be digitised and taken to open learning contexts.

Second is the McLuhan archive, where we create transmedia trace of a users journey through the archive, using Heidelberg Universities Tamboti platform and the platform Pandora - creating commentaries or glosses as publications.

Third is Moos Verlag, where we look to engage a new community with a 1970s urbanism publishing collection, this involves book scanning and re-publishing titles free online.

Last is Merve Verlag, where we are digitising 100 books from their back catalogue and will be making it available for remixing. The project was launched at the Frankfurt Book Fair in October at merve.consortium.io and will be developed by Merve as a free-2-air book remixing platform.

Thank you, any questions.

Slides

PDF version

Slide 1

Slide 2

Slide 3

Slide 4

Slide 5

Slide 6

Slide 7

Slide 8

Slide 9

Slide 10

Slide 11

INFO

↘

Contact: mail AT consortium.io

Imprint

© The authors | CC BY 3.0 DE

The Hybrid Publishing Consortium is a project of the Hybrid Publishing Lab in collaboration with partners and associates. The Hybrid Publishing Lab is part of the Leuphana University of Lüneburg Innovation Incubator, financed by the European Regional Development Fund and co-funded by the German federal state of Lower Saxony.


text:
"""
Test cases for Pet Model

Test cases can be run with:
    nosetests
    coverage report -m

While debugging just these tests it's convenient to use this:
    nosetests --stop tests/test_pets.py:TestPetModel

"""
import os
import logging
import unittest
from datetime import date
from werkzeug.exceptions import NotFound
from service.models import Pet, Gender, DataValidationError, db
from service import app
from tests.factories import PetFactory

DATABASE_URI = os.getenv(
    "DATABASE_URI", "postgresql://postgres:postgres@localhost:5432/testdb"
)

######################################################################
#  P E T   M O D E L   T E S T   C A S E S
######################################################################
# pylint: disable=too-many-public-methods
class TestPetModel(unittest.TestCase):
    """Test Cases for Pet Model"""

    @classmethod
    def setUpClass(cls):
        """This runs once before the entire test suite"""
        app.config["TESTING"] = True
        app.config["DEBUG"] = False
        app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI
        app.logger.setLevel(logging.CRITICAL)
        Pet.init_db(app)

    @classmethod
    def tearDownClass(cls):
        """This runs once after the entire test suite"""
        db.session.close()

    def setUp(self):
        """This runs before each test"""
        db.session.query(Pet).delete()  # clean up the last tests
        db.session.commit()

    def tearDown(self):
        """This runs after each test"""
        db.session.remove()

    ######################################################################
    #  T E S T   C A S E S
    ######################################################################

    def test_create_a_pet(self):
        """It should Create a pet and assert that it exists"""
        pet = Pet(name="Fido", category="dog", available=True, gender=Gender.MALE)
        self.assertEqual(str(pet), "<Pet Fido id=[None]>")
        self.assertTrue(pet is not None)
        self.assertEqual(pet.id, None)
        self.assertEqual(pet.name, "Fido")
        self.assertEqual(pet.category, "dog")
        self.assertEqual(pet.available, True)
        self.assertEqual(pet.gender, Gender.MALE)
        pet = Pet(name="Fido", category="dog", available=False, gender=Gender.FEMALE)
        self.assertEqual(pet.available, False)
        self.assertEqual(pet.gender, Gender.FEMALE)

    def test_add_a_pet(self):
        """It should Create a pet and add it to the database"""
        pets = Pet.all()
        self.assertEqual(pets, [])
        pet = Pet(name="Fido", category="dog", available=True, gender=Gender.MALE)
        self.assertTrue(pet is not None)
        self.assertEqual(pet.id, None)
        pet.create()
        # Assert that it was assigned an id and shows up in the database
        self.assertIsNotNone(pet.id)
        pets = Pet.all()
        self.assertEqual(len(pets), 1)

    def test_read_a_pet(self):
        """It should Read a Pet"""
        pet = PetFactory()
        logging.debug(pet)
        pet.id = None
        pet.create()
        self.assertIsNotNone(pet.id)
        # Fetch it back
        found_pet = Pet.find(pet.id)
        self.assertEqual(found_pet.id, pet.id)
        self.assertEqual(found_pet.name, pet.name)
        self.assertEqual(found_pet.category, pet.category)

    def test_update_a_pet(self):
        """It should Update a Pet"""
        pet = PetFactory()
        logging.debug(pet)
        pet.id = None
        pet.create()
        logging.debug(pet)
        self.assertIsNotNone(pet.id)
        # Change it an save it
        pet.category = "k9"
        original_id = pet.id
        pet.update()
        self.assertEqual(pet.id, original_id)
        self.assertEqual(pet.category, "k9")
        # Fetch it back and make sure the id hasn't changed
        # but the data did change
        pets = Pet.all()
        self.assertEqual(len(pets), 1)
        self.assertEqual(pets[0].id, original_id)
        self.assertEqual(pets[0].category, "k9")

    def test_update_no_id(self):
        """It should not Update a Pet with no id"""
        pet = PetFactory()
        logging.debug(pet)
        pet.id = None
        self.assertRaises(DataValidationError, pet.update)

    def test_delete_a_pet(self):
        """It should Delete a Pet"""
        pet = PetFactory()
        pet.create()
        self.assertEqual(len(Pet.all()), 1)
        # delete the pet and make sure it isn't in the database
        pet.delete()
        self.assertEqual(len(Pet.all()), 0)

    def test_list_all_pets(self):
        """It should List all Pets in the database"""
        pets = Pet.all()
        self.assertEqual(pets, [])
        # Create 5 Pets
        for _ in range(5):
            pet = PetFactory()
            pet.create()
        # See if we get back 5 pets
        pets = Pet.all()
        self.assertEqual(len(pets), 5)

    def test_serialize_a_pet(self):
        """It should serialize a Pet"""
        pet = PetFactory()
        data = pet.serialize()
        self.assertNotEqual(data, None)
        self.assertIn("id", data)
        self.assertEqual(data["id"], pet.id)
        self.assertIn("name", data)
        self.assertEqual(data["name"], pet.name)
        self.assertIn("category", data)
        self.assertEqual(data["category"], pet.category)
        self.assertIn("available", data)
        self.assertEqual(data["available"], pet.available)
        self.assertIn("gender", data)
        self.assertEqual(data["gender"], pet.gender.name)
        self.assertIn("birthday", data)
        self.assertEqual(date.fromisoformat(data["birthday"]), pet.birthday)

    def test_deserialize_a_pet(self):
        """It should de-serialize a Pet"""
        data = PetFactory().serialize()
        pet = Pet()
        pet.deserialize(data)
        self.assertNotEqual(pet, None)
        self.assertEqual(pet.id, None)
        self.assertEqual(pet.name, data["name"])
        self.assertEqual(pet.category, data["category"])
        self.assertEqual(pet.available, data["available"])
        self.assertEqual(pet.gender.name, data["gender"])
        self.assertEqual(pet.birthday, date.fromisoformat(data["birthday"]))

    def test_deserialize_missing_data(self):
        """It should not deserialize a Pet with missing data"""
        data = {"id": 1, "name": "Kitty", "category": "cat"}
        pet = Pet()
        self.assertRaises(DataValidationError, pet.deserialize, data)

    def test_deserialize_bad_data(self):
        """It should not deserialize bad data"""
        data = "this is not a dictionary"
        pet = Pet()
        self.assertRaises(DataValidationError, pet.deserialize, data)

    def test_deserialize_bad_available(self):
        """It should not deserialize a bad available attribute"""
        test_pet = PetFactory()
        data = test_pet.serialize()
        data["available"] = "true"
        pet = Pet()
        self.assertRaises(DataValidationError, pet.deserialize, data)

    def test_deserialize_bad_gender(self):
        """It should not deserialize a bad gender attribute"""
        test_pet = PetFactory()
        data = test_pet.serialize()
        data["gender"] = "male"  # wrong case
        pet = Pet()
        self.assertRaises(DataValidationError, pet.deserialize, data)

    def test_find_pet(self):
        """It should Find a Pet by ID"""
        pets = PetFactory.create_batch(5)
        for pet in pets:
            pet.create()
        logging.debug(pets)
        # make sure they got saved
        self.assertEqual(len(Pet.all()), 5)
        # find the 2nd pet in the list
        pet = Pet.find(pets[1].id)
        self.assertIsNot(pet, None)
        self.assertEqual(pet.id, pets[1].id)
        self.assertEqual(pet.name, pets[1].name)
        self.assertEqual(pet.available, pets[1].available)
        self.assertEqual(pet.gender, pets[1].gender)
        self.assertEqual(pet.birthday, pets[1].birthday)

    def test_find_by_category(self):
        """It should Find Pets by Category"""
        pets = PetFactory.create_batch(10)
        for pet in pets:
            pet.create()
        category = pets[0].category
        count = len([pet for pet in pets if pet.category == category])
        found = Pet.find_by_category(category)
        self.assertEqual(found.count(), count)
        for pet in found:
            self.assertEqual(pet.category, category)

    def test_find_by_name(self):
        """It should Find a Pet by Name"""
        pets = PetFactory.create_batch(5)
        for pet in pets:
            pet.create()
        name = pets[0].name
        found = Pet.find_by_name(name)
        self.assertEqual(found.count(), 1)
        self.assertEqual(found[0].category, pets[0].category)
        self.assertEqual(found[0].name, pets[0].name)
        self.assertEqual(found[0].available, pets[0].available)
        self.assertEqual(found[0].gender, pets[0].gender)
        self.assertEqual(found[0].birthday, pets[0].birthday)

    def test_find_by_availability(self):
        """It should Find Pets by Availability"""
        pets = PetFactory.create_batch(10)
        for pet in pets:
            pet.create()
        available = pets[0].available
        count = len([pet for pet in pets if pet.available == available])
        found = Pet.find_by_availability(available)
        self.assertEqual(found.count(), count)
        for pet in found:
            self.assertEqual(pet.available, available)

    def test_find_by_gender(self):
        """It should Find Pets by Gender"""
        pets = PetFactory.create_batch(10)
        for pet in pets:
            pet.create()
        gender = pets[0].gender
        count = len([pet for pet in pets if pet.gender == gender])
        found = Pet.find_by_gender(gender)
        self.assertEqual(found.count(), count)
        for pet in found:
            self.assertEqual(pet.gender, gender)

    def test_find_or_404_found(self):
        """It should Find or return 404 not found"""
        pets = PetFactory.create_batch(3)
        for pet in pets:
            pet.create()

        pet = Pet.find_or_404(pets[1].id)
        self.assertIsNot(pet, None)
        self.assertEqual(pet.id, pets[1].id)
        self.assertEqual(pet.name, pets[1].name)
        self.assertEqual(pet.available, pets[1].available)
        self.assertEqual(pet.gender, pets[1].gender)
        self.assertEqual(pet.birthday, pets[1].birthday)

    def test_find_or_404_not_found(self):
        """It should return 404 not found"""
        self.assertRaises(NotFound, Pet.find_or_404, 0)


text:
#include<stdlib.h>
/**
 * Definition for singly-linked list.
 */
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
		ListNode new_head(0);
		new_head.next = head;
		ListNode* old_ptr, * phead = &new_head;
		while(phead->next != NULL){
			if(phead->next->val == val){
				old_ptr = phead->next;
				phead->next = old_ptr->next;
				delete old_ptr;
			}else{
				phead = phead->next;
			}
		}
		return new_head.next;
    }
};


text:
package parser.symbol;

import parser.scope.Scope;

public class VariableSymbol extends Symbol {

	public VariableSymbol(Scope scope, int id, String name) {
		super(scope, id, name);
	}

}


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
null

#### Original name
Pertusaria muscicola Gorbatsch {?}

### Remarks
null


text:
package ws.epigraph.url.parser.psi.impl;

import java.util.List;
import org.jetbrains.annotations.*;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.util.PsiTreeUtil;
import static ws.epigraph.url.lexer.UrlElementTypes.*;
import com.intellij.extapi.psi.ASTWrapperPsiElement;
import ws.epigraph.url.parser.psi.*;
import ws.epigraph.lang.Qn;

public class UrlQnImpl extends ASTWrapperPsiElement implements UrlQn {

  public UrlQnImpl(ASTNode node) {
    super(node);
  }

  public void accept(@NotNull UrlVisitor visitor) {
    visitor.visitQn(this);
  }

  public void accept(@NotNull PsiElementVisitor visitor) {
    if (visitor instanceof UrlVisitor) accept((UrlVisitor)visitor);
    else super.accept(visitor);
  }

  @Override
  @NotNull
  public List<UrlQnSegment> getQnSegmentList() {
    return PsiTreeUtil.getChildrenOfTypeAsList(this, UrlQnSegment.class);
  }

  @NotNull
  public Qn getQn() {
    return UrlPsiImplUtil.getQn(this);
  }

}


text: puts "this is cartman's essay"


text:
package fredboat.command.music.control;

import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import fredboat.Config;
import fredboat.audio.player.GuildPlayer;
import fredboat.audio.player.PlayerRegistry;
import fredboat.audio.player.VideoSelection;
import fredboat.audio.queue.AudioTrackContext;
import fredboat.commandmeta.abs.Command;
import fredboat.commandmeta.abs.CommandContext;
import fredboat.commandmeta.abs.ICommandRestricted;
import fredboat.commandmeta.abs.IMusicCommand;
import fredboat.feature.I18n;
import fredboat.messaging.CentralMessaging;
import fredboat.perms.PermissionLevel;
import fredboat.util.TextUtils;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.exceptions.PermissionException;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;

public class SelectCommand extends Command implements IMusicCommand, ICommandRestricted {

    @Override
    public void onInvoke(CommandContext context) {
        select(context);
    }

    static void select(CommandContext context) {
        String[] args = context.args;
        Member invoker = context.invoker;
        GuildPlayer player = PlayerRegistry.get(context.guild);
        player.setCurrentTC(context.channel);
        if (player.selections.containsKey(invoker.getUser().getId())) {
            VideoSelection selection = player.selections.get(invoker.getUser().getId());
            try {
                int i = 1;

                if (args.length >= 1) {
                    String contentWithoutPrefix = args[0].substring(Config.CONFIG.getPrefix().length());
                    if (StringUtils.isNumeric(contentWithoutPrefix)) {
                        i = Integer.valueOf(contentWithoutPrefix);
                    } else {
                        i = Integer.valueOf(args[1]);
                    }
                }

                if (selection.getChoices().size() < i || i < 1) {
                    throw new NumberFormatException();
                } else {
                    AudioTrack selected = selection.getChoices().get(i - 1);
                    player.selections.remove(invoker.getUser().getId());
                    String msg = MessageFormat.format(I18n.get(context, "selectSuccess"), i, selected.getInfo().title, TextUtils.formatTime(selected.getInfo().length));
                    CentralMessaging.editMessageById(context.channel, selection.getOutMsgId(), CentralMessaging.from(msg));
                    player.queue(new AudioTrackContext(selected, invoker));
                    player.setPause(false);
                    try {
                        context.deleteMessage();
                    } catch (PermissionException ignored) {

                    }
                }
            } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                context.reply(MessageFormat.format(I18n.get(context, "selectInterval"), selection.getChoices().size()));
            }
        } else {
            context.reply(I18n.get(context, "selectSelectionNotGiven"));
        }
    }

    @Override
    public String help(Guild guild) {
        String usage = "{0}{1} n OR {0}{2} n\n#";
        return usage + I18n.get(guild).getString("helpSelectCommand");
    }

    @Override
    public PermissionLevel getMinimumPerms() {
        return PermissionLevel.USER;
    }
}


text:
.. _readme:

============
NiBetaSeries
============

If you are viewing this file on GitHub, please see our
`readthedocs page <https://nibetaseries.readthedocs.io>`_
for links to render properly.

.. start-badges

.. list-table::
    :stub-columns: 1

    * - docs
      - |docs| |joss|
    * - tests
      - | |travis| |circleci|
        | |codecov|
    * - binder
      - | |binder|
    * - package
      - | |version| |wheel| |supported-versions|
        | |supported-implementations| |zenodo|

.. |docs| image:: https://readthedocs.org/projects/nibetaseries/badge/?version=latest
    :alt: Documentation Status
    :target: https://nibetaseries.readthedocs.io/en/latest/?badge=latest

.. |zenodo| image:: https://zenodo.org/badge/DOI/10.5281/zenodo.2552303.svg
   :target: https://zenodo.org/record/2552303#.XFBjwN-YU8p

.. |joss| image:: https://joss.theoj.org/papers/10.21105/joss.01295/status.svg
   :target: https://doi.org/10.21105/joss.01295

.. |travis| image:: https://travis-ci.com/HBClab/NiBetaSeries.svg?branch=master
    :alt: Travis-CI Build Status
    :target: https://travis-ci.com/HBClab/NiBetaSeries

.. |codecov| image:: https://codecov.io/github/HBClab/NiBetaSeries/coverage.svg?branch=master
    :alt: Coverage Status
    :target: https://codecov.io/github/HBClab/NiBetaSeries

.. |circleci| image:: https://circleci.com/gh/HBClab/NiBetaSeries.svg?style=svg
    :alt: Circleci Build Status
    :target: https://circleci.com/gh/HBClab/NiBetaSeries

.. |binder| image:: https://mybinder.org/badge_logo.svg
    :target: https://mybinder.org/v2/gh/HBClab/NiBetaSeries/master?filepath=%2Fexamples

.. |version| image:: https://img.shields.io/pypi/v/nibetaseries.svg
    :alt: PyPI Package latest release
    :target: https://pypi.python.org/pypi/nibetaseries

.. |wheel| image:: https://img.shields.io/pypi/wheel/nibetaseries.svg
    :alt: PyPI Wheel
    :target: https://pypi.python.org/pypi/nibetaseries

.. |supported-versions| image:: https://img.shields.io/pypi/pyversions/nibetaseries.svg
    :alt: Supported versions
    :target: https://pypi.python.org/pypi/nibetaseries

.. |supported-implementations| image:: https://img.shields.io/pypi/implementation/nibetaseries.svg
    :alt: Supported implementations
    :target: https://pypi.python.org/pypi/nibetaseries

.. end-badges

What is NiBetaSeries?
---------------------
NiBetaSeries is BIDS_-compatible `application <https://bids-apps.neuroimaging.io/>`_
that calculates betaseries correlations.
In brief, a beta coefficient (i.e., parameter estimate) is calculated
for each trial (or event) resulting in a series of betas ("betaseries")
that can be correlated across regions of interest.

Why should I use it?
--------------------
There are potential insights hidden in your task fMRI data.
Rest fMRI enjoys a multitude of toolboxes which can be applied to task fMRI
with some effort, but there are not many toolboxes that focus on making
betaseries.
Betaseries can then be used for correlations/classifications and
a multitude of other analyses.
While a couple alternatives exist (pybetaseries_ and BASCO_), NiBetaSeries
is the only application to interface with BIDS_ organized data with the goal
of providing a command-line application experience like fMRIPrep_.

What does NiBetaSeries give me?
-------------------------------
Currently NiBetaSeries returns the beta series images and optionally
symmetric z-transformed correlation matrices with an entry for each
parcel defined in the atlas.

.. note:: The betas (i.e., parameter estimates) are generated using either
    the "Least Squares Separate" or "Least Squares All" procedures.
    Please read the :ref:`betaseries` page for more background information.

What do I need to run NiBetaSeries?
-----------------------------------
NiBetaSeries takes BIDS_ and preprocessed data as input that satisfy the
`BIDS derivatives specification <http://bit.ly/2vKeKcp>`_.
In practical terms, NiBetaSeries uses the output of fMRIPrep_,
a great BIDS-compatible preprocessing tool.
NiBetaSeries requires the input and the atlas to already
be in the same space (e.g., MNI space).
For more details, see :ref:`usage` and the tutorial
(:ref:`sphx_glr_auto_examples_plot_run_nibetaseries.py`)

Get Involved
------------
This is a very young project that still needs some tender loving care to grow.
That's where you fit in!
If you would like to contribute, please read our :ref:`code_of_conduct`
and contributing page (:ref:`contributing`).

Thanks!
-------
This project heavily leverages `nipype <http://nipype.readthedocs.io/en/latest/>`_,
`nilearn <https://nilearn.github.io/>`_, `pybids <https://bids-standard.github.io/pybids/>`_, and
`nistats <https://nistats.github.io/>`_ for development.
Please check out their pages and support the developers.

.. _BASCO: https://www.nitrc.org/projects/basco/
.. _pybetaseries: https://github.com/poldrack/pybetaseries
.. _BIDS: http://bids.neuroimaging.io/
.. _fMRIPrep: http://fmriprep.readthedocs.io/en/latest/


text:
package de.fred4jupiter.fredbet.service.image;

import de.fred4jupiter.fredbet.props.FredbetProperties;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * For resizing images and thumbnail generation.
 *
 * @author michael
 */
@Service
class ImageResizingService {

    private static final Logger log = LoggerFactory.getLogger(ImageResizingService.class);

    private final FredbetProperties fredbetProperties;

    public ImageResizingService(FredbetProperties fredbetProperties) {
        this.fredbetProperties = fredbetProperties;
    }

    public byte[] createThumbnail(byte[] imageBinary) {
        Integer thumbnailSize = fredbetProperties.getThumbnailSize();
        return minimizeToSize(imageBinary, thumbnailSize, builder -> builder.crop(Positions.CENTER).size(thumbnailSize, thumbnailSize));
    }

    public byte[] minimizeToSize(byte[] imageBinary, int size) {
        return minimizeToSize(imageBinary, size, builder -> builder.size(size, size));
    }

    public byte[] minimizeToSize(byte[] imageBinary, int size, ThumbnailsBuilderCallback byteArrayConverter) {
        if (size == 0) {
            throw new IllegalArgumentException("Given size must be greather than 0!");
        }
        try (ByteArrayInputStream byteIn = new ByteArrayInputStream(imageBinary);
             ByteArrayOutputStream byteOut = new ByteArrayOutputStream()) {

            BufferedImage bufferedImage = ImageIO.read(byteIn);
            if (bufferedImage.getWidth() <= size && bufferedImage.getHeight() <= size) {
                log.info(
                        "The original image does have the prefered size of {} or is smaller than that. Do not change image size. image with: {}, image height: {}",
                        size, bufferedImage.getWidth(), bufferedImage.getHeight());
                return imageBinary;
            }

            Builder<BufferedImage> builder = Thumbnails.of(bufferedImage);
            byteArrayConverter.doWithBuilder(builder);

            builder.outputFormat("JPEG").toOutputStream(byteOut);
            return byteOut.toByteArray();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    @FunctionalInterface
    private interface ThumbnailsBuilderCallback {

        void doWithBuilder(Builder<BufferedImage> builder);
    }
}


text:
<?php

namespace NordCode\RoboParameters\Serializer;

class SymfonyXmlSerializer extends XmlSerializer
{
    const CONTAINER_NAMESPACE = 'http://symfony.com/schema/dic/services';

    /**
     * @inheritDoc
     */
    protected function createDocumentElement(\DOMDocument $dom)
    {
        $documentElement = $dom->createElementNS(self::CONTAINER_NAMESPACE, 'container');
        $dom->appendChild($documentElement);
        $parameters = $dom->createElement('parameters');
        $documentElement->appendChild($parameters);
        return $parameters;
    }

    /**
     * @inheritDoc
     */
    protected function dumpArrayToNode(array $parameters, \DOMElement $parentNode, \DOMDocument $dom)
    {
        $isNumericalArray = $this->hasOnlyNumericalIndices($parameters);

        if ($isNumericalArray) {
            $parentNode->setAttribute('type', 'collection');
        }

        foreach ($parameters as $key => $parameter) {
            $node = $dom->createElement('parameter');
            if (!$isNumericalArray) {
                $node->setAttribute('key', $key);
            }
            $parentNode->appendChild($node);
            if (is_array($parameter)) {
                $this->dumpArrayToNode($parameter, $node, $dom);
            } else {
                $node->appendChild(new \DOMText($parameter));
            }
        }
    }

    /**
     * @param array $array
     * @return bool
     */
    private function hasOnlyNumericalIndices(array $array)
    {
        return array_keys($array) === range(0, count($array) - 1);
    }
}


text:
package org.ovirt.engine.core.bll.validator;

import org.ovirt.engine.core.bll.ValidationResult;
import org.ovirt.engine.core.common.businessentities.network.NetworkQoS;
import org.ovirt.engine.core.common.errors.VdcBllMessages;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
import org.ovirt.engine.core.dao.qos.QosDao;

public class NetworkQosValidator extends QosValidator<NetworkQoS> {

    public NetworkQosValidator(NetworkQoS qos) {
        super(qos);
    }

    /**
     * Verify that if any inbound/outbound capping was specified, that all three parameters are present.
     */
    @Override
    public ValidationResult allValuesPresent() {
        return (getQos() != null)
                && (missingValue(getQos().getInboundAverage(), getQos().getInboundPeak(), getQos().getInboundBurst())
                || missingValue(getQos().getOutboundAverage(), getQos().getOutboundPeak(), getQos().getOutboundBurst()))
                ? new ValidationResult(VdcBllMessages.ACTION_TYPE_FAILED_NETWORK_QOS_MISSING_VALUES)
                : ValidationResult.VALID;
    }

    private boolean missingValue(Integer average, Integer peak, Integer burst) {
        return (average != null || peak != null || burst != null) && (average == null || peak == null || burst == null);
    }

    /**
     * Verify that the specified peak value isn't lower than the specified average value.
     */
    public ValidationResult peakConsistentWithAverage() {
        return (getQos() != null) && (peakLowerThanAverage(getQos().getInboundAverage(), getQos().getInboundPeak())
                || peakLowerThanAverage(getQos().getOutboundAverage(), getQos().getOutboundPeak()))
                ? new ValidationResult(VdcBllMessages.ACTION_TYPE_FAILED_NETWORK_QOS_PEAK_LOWER_THAN_AVERAGE)
                : ValidationResult.VALID;
    }

    private boolean peakLowerThanAverage(Integer average, Integer peak) {
        return peak != null && peak < average;
    }

    @Override
    protected QosDao<NetworkQoS> getQosDao() {
        return DbFacade.getInstance().getNetworkQosDao();
    }

}


text:
package org.orekit.rugged.utils;

import org.hipparchus.util.FastMath;
import org.junit.Assert;
import org.junit.Test;

public class NormalizedGeodeticPointTest {

    /**
     * check {@link NormalizedGeodeticPoint#equals(Object)}.
     */
    @Test
    public void testEquals() {
        // setup
        NormalizedGeodeticPoint point = new NormalizedGeodeticPoint(1, 2, 3, 4);

        // actions + verify
        Assert.assertEquals(point, new NormalizedGeodeticPoint(1, 2, 3, 4));
        Assert.assertFalse(point.equals(new NormalizedGeodeticPoint(0, 2, 3, 4)));
        Assert.assertFalse(point.equals(new NormalizedGeodeticPoint(1, 0, 3, 4)));
        Assert.assertFalse(point.equals(new NormalizedGeodeticPoint(1, 2, 0, 4)));
        Assert.assertFalse(point.equals(new NormalizedGeodeticPoint(1, 2, 3, 10)));
        Assert.assertFalse(point.equals(new Object()));
    }

    /**
     * check {@link NormalizedGeodeticPoint#hashCode()}.
     */
    @Test
    public void testHashCode() {
        // setup
        NormalizedGeodeticPoint point = new NormalizedGeodeticPoint(1, 2, 3, 4);

        // actions + verify
        Assert.assertEquals(point.hashCode(), new NormalizedGeodeticPoint(1, 2, 3, 4).hashCode());
        Assert.assertNotEquals(point.hashCode(), new NormalizedGeodeticPoint(1, FastMath.nextUp(2), 3, 4).hashCode());
    }
}