com.github.carlomicieli.rest.representations.ScaleRepresentation.java Source code

Java tutorial

Introduction

Here is the source code for com.github.carlomicieli.rest.representations.ScaleRepresentation.java

Source

/*
 * Copyright 2012 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.carlomicieli.rest.representations;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.github.carlomicieli.entities.Scale;

/**
 * 
 * @author Carlo Micieli
 *
 */
public class ScaleRepresentation {
    private final Scale scale;

    /**
     * Creates new <em>Scale</em> representation.
     */
    public ScaleRepresentation() {
        this.scale = new Scale();
    }

    /**
     * Creates new <em>Scale</em> representation.
     * @param scale the inner scale entity object.
     */
    public ScaleRepresentation(Scale scale) {
        if (scale == null)
            throw new WebApplicationException(Status.NOT_FOUND);

        this.scale = scale;
    }

    /**
     * Creates new <em>Scale</em> representation.
     * @param formValues the form values.
     * @param formValues
     */
    public ScaleRepresentation(MultivaluedMap<String, String> formValues) {
        this();

        long id = 0;
        try {
            id = Long.parseLong(formValues.getFirst("id"));
        } catch (NumberFormatException ex) {
        }

        int ratio = 0;
        try {
            ratio = Integer.parseInt(formValues.getFirst("ratio"));
        } catch (NumberFormatException ex) {
            throw new WebApplicationException(Status.BAD_REQUEST);
        }

        boolean isNarrow = Boolean.parseBoolean(formValues.getFirst("narrow"));

        setId(id);
        setRatio(ratio);
        setName(formValues.getFirst("name"));
        setNarrow(isNarrow);
    }

    /**
     * Returns the inner scale entity object.
     * @return the entity object.
     */
    public Scale getInner() {
        return this.scale;
    }

    /**
     * Returns the unique id for the <em>Scale</em>.
     * @return the unique id.
     */
    public long getId() {
        return getInner().getId();
    }

    /**
     * Sets the unique id for the <em>Scale</em>.
     * @param id the unique id.
     */
    public void setId(long id) {
        getInner().setId(id);
    }

    /**
     * Returns the scale name.
     * @return the scale name.
     */
    public String getName() {
        return getInner().getName();
    }

    /**
     * Sets the scale name.
     * @param name the scale name.
     */
    public void setName(String name) {
        getInner().setName(name);
    }

    /**
     * Returns the ratio of a linear dimension of the 
     * model to the same dimension of the original.
     * @return the scale ratio.
     */
    public int getRatio() {
        return getInner().getRatio();
    }

    /**
     * Sets the ratio of a linear dimension of the 
     * model to the same dimension of the original.
     * @param ratio the scale ratio.
     */
    public void setRatio(int ratio) {
        getInner().setRatio(ratio);
    }

    /**
     * Returns whether has track gauge 
     * narrower than the standard gauge railways or not.
     * <p>
     * <em>true</em> if the scale is narrow, 
     * <em>false</em> otherwise.
     * </p>
     * 
     * @return the narrow flag value. 
     */
    public boolean isNarrow() {
        return getInner().isNarrow();
    }

    /**
     * Sets whether has track gauge 
     * narrower than the standard gauge railways or not.
     * @param isNarrow the narrow flag value.
     */
    public void setNarrow(boolean isNarrow) {
        getInner().setNarrow(isNarrow);
    }

    /**
     * Compute the ETag for the current player representation.
     * @return the ETag value.
     */
    public EntityTag tag() {
        return new EntityTag(Long.toHexString(this.hashCode()));
    }

    /**
     * Returns a string representation of this <em>BrandRepresentation</em>.
     * @return a string representation of the object.
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder(15, 37).append(getName()).append(getRatio()).append(isNarrow()).toHashCode();
    }
}