001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.feature;
031
032import java.io.DataInput;
033import java.io.DataOutput;
034import java.io.IOException;
035import java.io.PrintWriter;
036import java.util.Scanner;
037
038/**
039 * A feature-vector representation of an enumeration
040 *
041 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
042 *
043 * @param <T>
044 *            type of underlying enumeration
045 */
046public class EnumFV<T extends Enum<T>> implements FeatureVector {
047        private static final long serialVersionUID = 1L;
048        private T enumValue;
049
050        /**
051         * Construct the feature with the given value
052         *
053         * @param value
054         *            the value
055         */
056        public EnumFV(T value) {
057                this.enumValue = value;
058        }
059
060        @Override
061        public void readBinary(DataInput in) throws IOException {
062                try {
063                        @SuppressWarnings("unchecked")
064                        final Class<T> clz = (Class<T>) Class.forName(in.readUTF());
065                        final String name = in.readUTF();
066                        this.enumValue = Enum.valueOf(clz, name);
067                } catch (final ClassNotFoundException e) {
068                        throw new RuntimeException(e);
069                }
070        }
071
072        @Override
073        public void readASCII(Scanner in) throws IOException {
074                try {
075                        @SuppressWarnings("unchecked")
076                        final Class<T> clz = (Class<T>) Class.forName(in.nextLine());
077                        final String name = in.nextLine();
078                        this.enumValue = Enum.valueOf(clz, name);
079                } catch (final ClassNotFoundException e) {
080                        throw new RuntimeException(e);
081                }
082        }
083
084        @Override
085        public byte[] binaryHeader() {
086                return "EnFV".getBytes();
087        }
088
089        @Override
090        public String asciiHeader() {
091                return getClass().getName() + " ";
092        }
093
094        @Override
095        public void writeBinary(DataOutput out) throws IOException {
096                out.writeUTF(enumValue.getDeclaringClass().getName());
097                out.writeUTF(enumValue.toString());
098        }
099
100        @Override
101        public void writeASCII(PrintWriter out) throws IOException {
102                out.println(enumValue.getDeclaringClass().getName());
103                out.println(enumValue.toString());
104        }
105
106        @Override
107        public Enum<T> getVector() {
108                return enumValue;
109        }
110
111        @Override
112        public int length() {
113                return enumValue.getDeclaringClass().getEnumConstants().length;
114        }
115
116        @Override
117        public DoubleFV normaliseFV(double[] min, double[] max) {
118                return asDoubleFV().normaliseFV(min, max);
119        }
120
121        @Override
122        public DoubleFV normaliseFV(double min, double max) {
123                return asDoubleFV().normaliseFV(min, max);
124        }
125
126        @Override
127        public DoubleFV normaliseFV() {
128                return asDoubleFV().normaliseFV();
129        }
130
131        @Override
132        public DoubleFV normaliseFV(double p) {
133                return asDoubleFV().normaliseFV(p);
134        }
135
136        @Override
137        public DoubleFV asDoubleFV() {
138                return new DoubleFV(asDoubleVector());
139        }
140
141        @Override
142        public double[] asDoubleVector() {
143                final double[] vec = new double[length()];
144                vec[enumValue.ordinal()] = 1;
145                return vec;
146        }
147
148        @Override
149        public double getAsDouble(int i) {
150                if (enumValue.ordinal() == i)
151                        return 1;
152                return 0;
153        }
154
155        @SuppressWarnings("unchecked")
156        @Override
157        public void setFromDouble(int i, double v) {
158                if (v == 1) {
159                        try {
160                                enumValue = (T) this.getClass().getField("enumValue").getType().getEnumConstants()[i];
161                        } catch (final Exception e) {
162                                throw new RuntimeException(e);
163                        }
164                }
165        }
166
167        @Override
168        public EnumFV<T> newInstance() {
169                return new EnumFV<T>(enumValue);
170        }
171}