package com.github.lstephen.ootp.extract.html.rating;

import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;

/**
 *
 * @author lstephen
 */
public final class Rating<T, S extends Scale<T>> {

    private final T value;

    private final S scale;

    private Rating(T value, S scale) {
        Preconditions.checkNotNull(value);
        Preconditions.checkNotNull(scale);

        // Seems that occaisionally a String gets through here.
        // This fix hides a problem somewhere else.
        // The error occurs when loading from JSON
        this.value = (String.class.isInstance(value)
            ? scale.parse(String.class.cast(value)).value
            : value);

        this.scale = scale;
    }

    @JsonValue
    public T get() {
        return value;
    }

    public Rating<Integer, OneToOneHundred> normalize() {
        try {
            return scale.normalize(value);
        } catch (ClassCastException e) {
            System.out.println(scale + ":" + value);
            throw Throwables.propagate(e);
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }

        Rating<?, ?> rhs = Rating.class.cast(obj);

        return Objects.equal(value, rhs.value)
            && Objects.equal(scale, rhs.scale);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(value, scale);
    }

    @Override
    public String toString() {
        return Objects
            .toStringHelper(this)
            .add("value", value)
            .add("scale", scale)
            .toString();
    }

    public static <T, S extends Scale<T>> Rating<T, S> create(
        T value, S scale) {

        return new Rating<T, S>(value, scale);
    }

}

@@ -48,16 +48,82 @@
 ating;%0A%0A
+import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0A
 import c
@@ -165,16 +165,17 @@
 nValue;%0A
+%0A
 import c
@@ -372,24 +372,108 @@
 cale%3CT%3E%3E %7B%0A%0A
+    private static final Logger LOGGER = Logger.getLogger(Rating.class.getName());%0A%0A
     private 
@@ -654,233 +654,226 @@
     
-// Seems that occaisionally a String gets through here.%0A        // This fix hides a problem somewhere else.%0A        // The error occurs when loading from JSON%0A        this.value = (String.class.isInstance(value)%0A            ?
+if (String.class.isInstance(value)) %7B%0A          Exception backtrace = new RuntimeException();%0A          LOGGER.log(Level.WARNING, %22Received String for Rating: %22 + value + %22:%22 + scale, backtrace);%0A          this.value =
  sca
@@ -908,24 +908,25 @@
 alue)).value
+;
 %0A           
@@ -926,21 +926,56 @@
     
-    : value);
+%7D else %7B%0A          this.value = value;%0A        %7D
 %0A%0A  

Add log message for Rating created in a bad way