001/*
002        AUTOMATICALLY GENERATED BY jTemp FROM
003        /Users/jsh2/Work/openimaj/target/checkout/core/core/src/main/jtemp/org/openimaj/io/wrappers/ReadWriteable#T#.jtemp
004*/
005/**
006 * Copyright (c) 2011, The University of Southampton and the individual contributors.
007 * All rights reserved.
008 *
009 * Redistribution and use in source and binary forms, with or without modification,
010 * are permitted provided that the following conditions are met:
011 *
012 *   *  Redistributions of source code must retain the above copyright notice,
013 *      this list of conditions and the following disclaimer.
014 *
015 *   *  Redistributions in binary form must reproduce the above copyright notice,
016 *      this list of conditions and the following disclaimer in the documentation
017 *      and/or other materials provided with the distribution.
018 *
019 *   *  Neither the name of the University of Southampton nor the names of its
020 *      contributors may be used to endorse or promote products derived from this
021 *      software without specific prior written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
027 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
030 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033 */
034package org.openimaj.io.wrappers;
035
036import java.io.DataInput;
037import java.io.DataOutput;
038import java.io.IOException;
039import java.io.PrintWriter;
040import java.util.Scanner;
041
042import org.openimaj.io.ReadWriteable;
043
044/**
045 * A wrapper around byte that can be read and written.
046 * 
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
049 *
050 */
051public class ReadWriteableByte implements ReadWriteable, Comparable<ReadWriteableByte> {
052        /**
053         * The underlying value
054         */
055        public byte value;
056        
057        /**
058         * Construct the wrapper with the given value.
059         * 
060         * @param value the value
061         */
062        public ReadWriteableByte(byte value) {
063                this.value = value;
064        }
065
066        /**
067         * Construct the wrapper with the value of 0.
068         */
069        public ReadWriteableByte() {
070                value = 0;
071        }
072        
073        /* (non-Javadoc)
074         * @see org.openimaj.io.Readable#readBinary(java.io.DataInput)
075         */
076        @Override
077        public void readBinary(DataInput in) throws IOException {
078                value = in.readByte();
079        }
080
081        /* (non-Javadoc)
082         * @see org.openimaj.io.Readable#readASCII(java.util.Scanner)
083         */
084        @Override
085        public void readASCII(Scanner in) throws IOException {
086                value = in.nextByte();
087        }
088
089        /* (non-Javadoc)
090         * @see org.openimaj.io.Readable#binaryHeader()
091         */
092        @Override
093        public byte[] binaryHeader() {
094                return new byte[0];
095        }
096
097        /* (non-Javadoc)
098         * @see org.openimaj.io.Readable#asciiHeader()
099         */
100        @Override
101        public String asciiHeader() {
102                return "";
103        }
104
105        /* (non-Javadoc)
106         * @see org.openimaj.io.Writeable#writeBinary(java.io.DataOutput)
107         */
108        @Override
109        public void writeBinary(DataOutput out) throws IOException {
110                out.writeByte(value);
111        }
112
113        /* (non-Javadoc)
114         * @see org.openimaj.io.Writeable#writeASCII(java.io.PrintWriter)
115         */
116        @Override
117        public void writeASCII(PrintWriter out) throws IOException {
118                out.print(value + "\n");
119        }
120        
121        /* (non-Javadoc)
122         * @see java.lang.Object#equals(java.lang.Object)
123         */
124        @Override
125        public boolean equals(Object o) {
126                return o instanceof ReadWriteableByte && ((ReadWriteableByte)o).value == this.value;
127        }
128        
129        /* (non-Javadoc)
130         * @see java.lang.Object#hashCode()
131         */
132        @Override
133        public int hashCode(){
134                return new Byte(value).hashCode();
135        }
136
137        /* (non-Javadoc)
138         * @see java.lang.Comparable#compareTo(java.lang.Object)
139         */
140        @Override
141        public int compareTo(ReadWriteableByte arg0) {
142                return new Byte(value).compareTo(arg0.value);
143        }
144}