/* 
 * Copyright (c) 2008 Stiftung Deutsches Elektronen-Synchrotron, 
 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
 *
 * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS. 
 * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND 
 * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE 
 * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR 
 * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. 
 * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
 * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
 * OR MODIFICATIONS.
 * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION, 
 * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS 
 * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY 
 * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
 */
 package org.csstudio.platform.internal.data;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.csstudio.platform.data.IEnumeratedMetaData;
import org.csstudio.platform.data.IEnumeratedValue;
import org.csstudio.platform.data.ISeverity;
import org.csstudio.platform.data.ITimestamp;

/** Implementation of {@link IEnumeratedValue}.
 *  @see IEnumeratedValue
 *  @author Kay Kasemir, Xihui Chen
 */
public class EnumeratedValue extends Value implements IEnumeratedValue
{
    final private int values[];
	
    /** Constructor from pieces. */
	public EnumeratedValue(ITimestamp time, ISeverity severity, String status,
                    IEnumeratedMetaData meta_data, Quality quality,
                    int values[])
	{
		super(time, severity, status, meta_data, quality);
		this.values = values;
	}

    /** {@inheritDoc} */
	final public int[] getValues()
	{	return values;	}

    /** {@inheritDoc} */
	final public int getValue()
	{	return values[0];  }
	
    /** {@inheritDoc} */
	@Override
	final public String format(final Format how, final int precision)
	{
	    final IEnumeratedMetaData enum_meta = (IEnumeratedMetaData)getMetaData();
	    final StringBuffer buf = new StringBuffer();
		if (getSeverity().hasValue())
		{
			if(how == Format.Default){
				buf.append(enum_meta.getState(values[0]));
				for (int i = 1; i < values.length; i++)
				{
					buf.append(Messages.ArrayElementSeparator);
		            buf.append(enum_meta.getState(values[i]));
				}
			}else if (how == Format.Decimal){
				buf.append(values[0]);
				for (int i = 1; i < values.length; i++)
				{
					buf.append(Messages.ArrayElementSeparator);
		            buf.append(values[i]);
				}
			}else if (how == Format.Exponential){
				// Is there a better way to get this silly format?
			 	NumberFormat fmt;
                StringBuffer pattern = new StringBuffer(10);
                pattern.append("0."); //$NON-NLS-1$
                for (int i=0; i<precision; ++i)
                    pattern.append('0');
                pattern.append("E0"); //$NON-NLS-1$
                fmt = new DecimalFormat(pattern.toString());
                buf.append(fmt.format(values[0]));
                for (int i = 1; i < values.length; i++)
	        	 {
	        		 buf.append(Messages.ArrayElementSeparator);
	        		 buf.append(buf.append(fmt.format(values[i])));
	        	 }
			}
			
		}
		else
			buf.append(Messages.NoValue);
		return buf.toString();
	}
	
    /** {@inheritDoc} */
	@Override
	final public boolean equals(final Object obj)
	{
		if (! (obj instanceof EnumeratedValue))
			return false;
		final EnumeratedValue rhs = (EnumeratedValue) obj;
		if (rhs.values.length != values.length)
			return false;
		for (int i=0; i<values.length; ++i)
			if (rhs.values[i] != values[i])
				return false;
		return super.equals(obj);
	}

    /** {@inheritDoc} */
	@Override
	final public int hashCode()
	{
		int h = super.hashCode();
		for (int i=0; i<values.length; ++i)
			h += values[i];
		return h;
	}
}

@@ -2641,16 +2641,40 @@
 .Default
+ %7C%7C how == Format.String
 )%7B%0A%09%09%09%09b

handle string format