/*******************************************************************************
 * Copyright (c) 2014 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
package org.csstudio.display.pvtable.model;

import java.util.logging.Level;

import org.csstudio.display.pvtable.Plugin;
import org.epics.pvmanager.PV;
import org.epics.vtype.VDouble;
import org.epics.vtype.VNumber;
import org.epics.vtype.VString;
import org.epics.vtype.VType;

/** Saved value of a table item
 * 
 *  <p>Values are always saved as String.
 *  When reading autosave-files, the PV's data type
 *  is unknown until the channel connects,
 *  and a channel may never connect.
 *  To allow reading and writing files without
 *  changing the exact value format, the text
 *  is kept.
 *
 *  @author Kay Kasemir
 */
public class SavedValue
{
    final private String saved_value;
    
    /** Initialize from text
     *  @param value_text
     */
    public SavedValue(final String value_text)
    {
        saved_value = value_text;
    }
    
    /** Initialize from value
     *  @param current_value
     */
    public SavedValue(final VType current_value)
    {
        saved_value = VTypeHelper.getValue(current_value).toString();
    }

    /** Compare saved value against current value of a PV
     *  @param current_value Value to compare against
     *  @param tolerance Tolerance to use for numeric values
     *  @return
     */
    public boolean isEqualTo(final VType current_value, final double tolerance)
    {
        if (current_value == null)
            return true;
        try
        {
            if (current_value instanceof VNumber)
            {
                final double v1 = ((VDouble)current_value).getValue();
                final double v2 = Double.parseDouble(saved_value);
                return Math.abs(v2 - v1) <= tolerance;
            }
            if (current_value instanceof VString)
            {
                final long v1 = ((VNumber)current_value).getValue().longValue();
                final long v2 = Long.parseLong(saved_value);
                return Math.abs(v2 - v1) <= tolerance;
            }
            // TODO VEnum
            // TODO Array classes
            throw new Exception("Cannot compare against unhandled type " + current_value.getClass().getName());
        }
        catch (Throwable ex)
        {
            Plugin.getLogger().log(Level.WARNING,
                "Comparison error for saved value " + saved_value + " and " + current_value, ex);
        }
        return false;
    }
    
    /** Restore saved value to PV
     *  @param pv PV to write
     */
    public void restore(final PV<VType, Object> pv)
    {
        // Determine what type to write based on current value of the PV
        final VType pv_type = pv.getValue();
        if (pv_type instanceof VDouble)
            pv.write(Double.parseDouble(saved_value));
        else if (pv_type instanceof VNumber)
            pv.write(Long.parseLong(saved_value));
        else // Write as text
            pv.write(saved_value);
    }
    
    /** @return String representation for display purpose */
    @Override
    public String toString()
    {
        return saved_value;
    }
}

@@ -639,16 +639,46 @@
 Double;%0A
+import org.epics.vtype.VEnum;%0A
 import o
@@ -2006,22 +2006,22 @@
 v1 = ((V
-Double
+Number
 )current
@@ -2030,32 +2030,46 @@
 alue).getValue()
+.doubleValue()
 ;%0A              
@@ -2272,26 +2272,28 @@
       final 
-lo
+Stri
 ng v1 = ((VN
@@ -2283,38 +2283,38 @@
  String v1 = ((V
-Number
+String
 )current_value).
@@ -2327,18 +2327,195 @@
 ue()
-.longValue
+;%0A                return v1.equals(saved_value);%0A            %7D%0A            if (current_value instanceof VEnum)%0A            %7B%0A                final int v1 = ((VEnum)current_value).getIndex
 ();%0A
@@ -2540,32 +2540,33 @@
 nal 
-long
+int
  v2 = 
-Long
+Integer
 .parse
-Long
+Int
 (sav
@@ -2649,34 +2649,8 @@
   %7D%0A
-            // TODO VEnum%0A
     

Fix (new) comparison