/*
 * 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 attribute's name.
     * @return The attribute's name.
     */
    @Nonnull
    public String getName() {
        return name;
    }

    /**
     * Get the attribute's type.  This will never be a primitive type class; primitive classes are
     * always normalized to their wrapper classes (e.g. `long.class` becomes `Long.class`).
     *
     * @return The attribute'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 attribute object.
     *
     * @param name The attribute name.
     * @param type The attribute type.
     * @return An object encapsulating the specified attribute.
     */
    @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("attributes must use serialization proxy");
    }

    private void readObjectNoData() throws ObjectStreamException {
        throw new InvalidObjectException("attributes 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());
            }
        }
    }
}

@@ -2587,19 +2587,8 @@
     
- hashCode =
  hc 
@@ -2606,27 +2606,16 @@
 d(name)%0A
-           
         
@@ -2651,32 +2651,48 @@
                 
+ .toHashCode();%0A
             .toH
@@ -2683,37 +2683,37 @@
             
-.toH
+h
 ashCode
-()
+ = hc
 ;%0A        %7D%0A

Remove double-assignment