/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2014 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.lenskit.data.entities;

import com.google.common.base.Preconditions;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.grouplens.grapht.util.ClassProxy;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.io.*;

/**
 * Identifier for an attribute in an entity.
 */
@Immutable
public final class Attribute<T> implements Serializable {
    private static final long serialVersionUID = -1L;
    private static final Interner<Attribute<?>> FIELD_CACHE = Interners.newStrongInterner();

    private final String name;
    private final Class<T> type;
    private transient volatile int hashCode;

    private Attribute(String n, Class<T> t) {
        name = n;
        type = t;
    }

    /**
     * Get the field's name.
     * @return The field's name.
     */
    @Nonnull
    public String getName() {
        return name;
    }

    /**
     * Get the field's type.  Primitive field types are normalized to their wrapper types, so this is always a reference
     * type.
     *
     * @return The field's type.
     */
    @Nonnull
    public Class<T> getType() {
        return type;
    }

    @Override
    public int hashCode() {
        // intermediate variable means only 1 memory fence in common case
        int hc = hashCode;
        if (hc == 0) {
            HashCodeBuilder hcb = new HashCodeBuilder();
            hashCode = hc = hcb.append(name)
                               .append(type)
                               .toHashCode();
        }
        return hc;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (o instanceof Attribute) {
            Attribute of = (Attribute) o;
            return name.equals(of.getName()) && type.equals(of.getType());
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        String tname;
        if (ClassUtils.isPrimitiveWrapper(type)) {
            tname = ClassUtils.wrapperToPrimitive(type).getName();
        } else {
            tname = type.getCanonicalName();
        }
        return "Attribute[" + name + ": " + tname + "]";
    }

    /**
     * Create a field object.
     *
     * @param name The field name.
     * @param type The field type.
     * @return An object encapsulating the specified field.
     */
    @SuppressWarnings("unchecked")
    @Nonnull
    public static <T> Attribute<T> create(String name, Class<T> type) {
        Preconditions.checkNotNull(name, "attribute name");
        Preconditions.checkNotNull(type, "attribute type");
        if (type.isPrimitive()) {
            type = (Class<T>) ClassUtils.primitiveToWrapper(type);
        }
        Attribute<T> attribute = new Attribute<>(name.intern(), type);
        return (Attribute<T>) FIELD_CACHE.intern(attribute);
    }

    private void readObject(ObjectInputStream in) throws IOException {
        throw new InvalidObjectException("fields must use serialization proxy");
    }

    private void readObjectNoData() throws ObjectStreamException {
        throw new InvalidObjectException("fields must use serialization proxy");
    }

    private Object writeReplace() {
        return new SerialProxy(name, ClassProxy.of(type));
    }

    private static class SerialProxy implements Serializable {
        private static final long serialVersionUID = 1L;

        private String name;
        private ClassProxy type;

        public SerialProxy(String n, ClassProxy t) {
            name = n;
            type = t;
        }

        private Object readResolve() throws ObjectStreamException {
            try {
                return create(name, type.resolve());
            } catch (ClassNotFoundException e) {
                throw new InvalidObjectException("Cannot resolve type " + type.getClassName());
            }
        }
    }
}

@@ -1891,21 +1891,25 @@
 Get the 
-field
+attribute
 's name.
@@ -1928,21 +1928,25 @@
 urn The 
-field
+attribute
 's name.
@@ -2048,21 +2048,25 @@
 Get the 
-field
+attribute
 's type.
@@ -2071,110 +2071,163 @@
 e.  
-Primitive field types are normalized to their wrapper types, so this is always a reference%0A     * type
+This will never be a primitive type class; primitive classes are%0A     * always normalized to their wrapper classes (e.g. %60long.class%60 becomes %60Long.class%60)
 .%0A  
@@ -2250,21 +2250,25 @@
 urn The 
-field
+attribute
 's type.
@@ -3413,21 +3413,25 @@
 reate a 
-field
+attribute
  object.
@@ -3461,21 +3461,25 @@
 ame The 
-field
+attribute
  name.%0A 
@@ -3500,21 +3500,25 @@
 ype The 
-field
+attribute
  type.%0A 
@@ -3569,21 +3569,25 @@
 ecified 
-field
+attribute
 .%0A     *
@@ -4183,37 +4183,41 @@
 bjectException(%22
-field
+attribute
 s must use seria
@@ -4354,13 +4354,17 @@
 on(%22
-field
+attribute
 s mu

Improve Attribute JavaDoc