<commit_before>/**
 * Copyright (C) 2010-12 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.vtype;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.epics.util.array.ListNumber;
import org.epics.util.text.NumberFormats;
import org.epics.util.time.TimestampFormat;

/**
 * Various utility methods for runtime handling of the types defined in
 * this package.
 *
 * @author carcassi
 */
public class ValueUtil {

    private ValueUtil() {
        // Can't instanciate
    }

    private static Collection<Class<?>> types = Arrays.<Class<?>>asList(VByte.class, VByteArray.class, VDouble.class,
            VDoubleArray.class, VEnum.class, VEnumArray.class, VFloat.class, VFloatArray.class,
            VInt.class, VIntArray.class, VMultiDouble.class, VMultiEnum.class,
            VMultiInt.class, VMultiString.class, VShort.class, VShortArray.class,
            VStatistics.class, VString.class, VStringArray.class, VBoolean.class, VTable.class);

    /**
     * Returns the type of the object by returning the class object of one
     * of the VXxx interfaces. The getClass() methods returns the
     * concrete implementation type, which is of little use. If no
     * super-interface is found, Object.class is used.
     * 
     * @param obj an object implementing a standard type
     * @return the type is implementing
     */
    public static Class<?> typeOf(Object obj) {
        if (obj == null)
            return null;

        return typeOf(obj.getClass());
    }

    private static Class<?> typeOf(Class<?> clazz) {
        if (clazz.equals(Object.class))
            return Object.class;

        for (int i = 0; i < clazz.getInterfaces().length; i++) {
            Class<?> interf = clazz.getInterfaces()[i];
            if (types.contains(interf))
                return interf;
        }

        return typeOf(clazz.getSuperclass());
    }

    /**
     * Extracts the alarm information if present.
     *
     * @param obj an object implementing a standard type
     * @return the alarm information for the object
     */
    public static Alarm alarmOf(Object obj) {
        if (obj == null) {
            return ValueFactory.alarmNone();
        }
        if (obj instanceof Alarm)
            return (Alarm) obj;
        return null;
    }
    
    /**
     * Extracts the alarm information if present, based on value
     * and connection status.
     * 
     * @param value a value
     * @param connected the connection status
     * @return the alarm information
     */
    public static Alarm alarmOf(Object value, boolean connected) {
        if (value != null) {
            return alarmOf(value);
        } else if (connected) {
            return ValueFactory.newAlarm(AlarmSeverity.INVALID, "No value");
        } else {
            return ValueFactory.newAlarm(AlarmSeverity.UNDEFINED, "Disconnected");
        }
    }

    /**
     * Extracts the time information if present.
     *
     * @param obj an object implementing a standard type
     * @return the time information for the object
     */
    public static Time timeOf(Object obj) {
        if (obj instanceof Time)
            return (Time) obj;
        return null;
    }

    /**
     * Extracts the display information if present.
     *
     * @param obj an object implementing a standard type
     * @return the display information for the object
     */
    public static Display displayOf(Object obj) {
        if (!(obj instanceof Display))
            return null;
        Display display = (Display) obj;<commit_msg>vtype: fix alarm logic If you have a value, that has no alarm, there is no alarm.<commit_after>/**
 * Copyright (C) 2010-12 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */
package org.epics.vtype;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.epics.util.array.ListNumber;
import org.epics.util.text.NumberFormats;
import org.epics.util.time.TimestampFormat;

/**
 * Various utility methods for runtime handling of the types defined in
 * this package.
 *
 * @author carcassi
 */
public class ValueUtil {

    private ValueUtil() {
        // Can't instanciate
    }

    private static Collection<Class<?>> types = Arrays.<Class<?>>asList(VByte.class, VByteArray.class, VDouble.class,
            VDoubleArray.class, VEnum.class, VEnumArray.class, VFloat.class, VFloatArray.class,
            VInt.class, VIntArray.class, VMultiDouble.class, VMultiEnum.class,
            VMultiInt.class, VMultiString.class, VShort.class, VShortArray.class,
            VStatistics.class, VString.class, VStringArray.class, VBoolean.class, VTable.class);

    /**
     * Returns the type of the object by returning the class object of one
     * of the VXxx interfaces. The getClass() methods returns the
     * concrete implementation type, which is of little use. If no
     * super-interface is found, Object.class is used.
     * 
     * @param obj an object implementing a standard type
     * @return the type is implementing
     */
    public static Class<?> typeOf(Object obj) {
        if (obj == null)
            return null;

        return typeOf(obj.getClass());
    }

    private static Class<?> typeOf(Class<?> clazz) {
        if (clazz.equals(Object.class))
            return Object.class;

        for (int i = 0; i < clazz.getInterfaces().length; i++) {
            Class<?> interf = clazz.getInterfaces()[i];
            if (types.contains(interf))
                return interf;
        }

        return typeOf(clazz.getSuperclass());
    }

    /**
     * Extracts the alarm information if present.
     *
     * @param obj an object implementing a standard type
     * @return the alarm information for the object
     */
    public static Alarm alarmOf(Object obj) {
        if (obj == null) {
            return ValueFactory.alarmNone();
        }
        if (obj instanceof Alarm)
            return (Alarm) obj;
        return null;
    }
    
    /**
     * Extracts the alarm information if present, based on value
     * and connection status.
     * 
     * @param value a value
     * @param connected the connection status
     * @return the alarm information
     */
    public static Alarm alarmOf(Object value, boolean connected) {
        if (value != null) {
            if (value instanceof Alarm) {
                return (Alarm) value;
            } else {
                return ValueFactory.alarmNone();
            }
        } else if (connected) {
            return ValueFactory.newAlarm(AlarmSeverity.INVALID, "No value");
        } else {
            return ValueFactory.newAlarm(AlarmSeverity.UNDEFINED, "Disconnected");
        }
    }

    /**
     * Extracts the time information if present.
     *
     * @param obj an object implementing a standard type
     * @return the time information for the object
     */
    public static Time timeOf(Object obj) {
        if (obj instanceof Time)
            return (Time) obj;
        return null;
    }

    /**
     * Extracts the display information if present.
     *
     * @param obj an object implementing a standard type
     * @return the display information for the object
     */
    public static Display displayOf(Object obj) {
        if (!(obj instanceof Display))
            return null;
        Display display = (Display) obj;
{'fraction_non_alphanumeric': 0.0950575148527367, 'fraction_numerical': 0.0017696877765137151, 'mean_word_length': 2.400085947571981, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7555101', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2158, 'n_words': 703}