openlr.binary.data.Offset.java Source code

Java tutorial

Introduction

Here is the source code for openlr.binary.data.Offset.java

Source

/**
 * Licensed to the TomTom International B.V. under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  TomTom International B.V.
 * licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
/**
 *  Copyright (C) 2009-2012 TomTom International B.V.
 *
 *   TomTom (Legal Department)
 *   Email: legal@tomtom.com
 *
 *   TomTom (Technical contact)
 *   Email: openlr@tomtom.com
 *
 *   Address: TomTom International B.V., Oosterdoksstraat 114, 1011DK Amsterdam,
 *   the Netherlands
 */
package openlr.binary.data;

import openlr.binary.bitstream.BitstreamException;
import openlr.binary.bitstream.BitstreamInput;
import openlr.binary.bitstream.BitstreamOutput;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.log4j.Logger;

/**
 * The Class Offset holds offset information as defined in the physical data
 * format for OpenLR. See OpenLR white paper for additional information.
 * 
 * <p>
 * OpenLR is a trade mark of TomTom International B.V.
 * <p>
 * email: software@openlr.org
 * 
 * @author TomTom International B.V.
 */
public class Offset extends OpenLRBinaryInformation {

    /** logger */
    private static final Logger LOG = Logger.getLogger(Offset.class);

    /** number of bits used for offset */
    private static final int OFFSET_BITS = 8;

    /** The offset information. */
    private final int offset;

    /**
     * Instantiates a new offset.
     *
     * @param o the o
     */
    public Offset(final int o) {
        offset = o;
    }

    /**
     * Fills the internal values with the values received from the input stream.
     * 
     * @param ibs
     *            the input stream to read from
     * 
     * @throws BitstreamException
     *             if a bitstream handling error occurred
     */
    public Offset(final BitstreamInput ibs) throws BitstreamException {
        offset = ibs.getBits(OFFSET_BITS);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Offset filled with data: offset - " + offset);
        }
    }

    /** {@inheritDoc} */
    public final void put(final BitstreamOutput obs) throws BitstreamException {
        obs.putBits(offset, OFFSET_BITS);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Offset data written: offset - " + offset);
        }
    }

    /**
     * Gets the offset information.
     * 
     * @return the offset
     */
    public final int getOffset() {
        return offset;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("offset: ").append(offset);
        return sb.toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int hashCode() {
        HashCodeBuilder builder = new HashCodeBuilder();
        builder.append(offset);
        return builder.toHashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean equals(final Object obj) {
        if (!(obj instanceof Offset)) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        Offset other = (Offset) obj;
        EqualsBuilder builder = new EqualsBuilder();
        builder.append(offset, other.offset);
        return builder.isEquals();
    }

}