001package jmri.jmrit.logixng.expressions; 002 003import java.text.NumberFormat; 004import java.util.Locale; 005 006import java.util.Map; 007 008import jmri.InstanceManager; 009import jmri.jmrit.logixng.*; 010 011/** 012 * Constant value. 013 * This can be useful for example by the ActionThrottle. 014 * 015 * @author Daniel Bergqvist Copyright 2019 016 */ 017public class AnalogExpressionConstant extends AbstractAnalogExpression { 018 019 private double _value; 020 021 public AnalogExpressionConstant(String sys, String user) 022 throws BadUserNameException, BadSystemNameException { 023 024 super(sys, user); 025 } 026 027 @Override 028 public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) { 029 AnalogExpressionManager manager = InstanceManager.getDefault(AnalogExpressionManager.class); 030 String sysName = systemNames.get(getSystemName()); 031 String userName = userNames.get(getSystemName()); 032 if (sysName == null) sysName = manager.getAutoSystemName(); 033 AnalogExpressionConstant copy = new AnalogExpressionConstant(sysName, userName); 034 copy.setComment(getComment()); 035 copy.setValue(_value); 036 return manager.registerExpression(copy); 037 } 038 039 /** {@inheritDoc} */ 040 @Override 041 public Category getCategory() { 042 return Category.ITEM; 043 } 044 045 public void setValue(double value) { 046 assertListenersAreNotRegistered(log, "setValue"); 047 _value = value; 048 } 049 050 public double getValue() { 051 return _value; 052 } 053 054 /** {@inheritDoc} */ 055 @Override 056 public double evaluate() { 057 return _value; 058 } 059 060 @Override 061 public FemaleSocket getChild(int index) 062 throws IllegalArgumentException, UnsupportedOperationException { 063 throw new UnsupportedOperationException("Not supported."); 064 } 065 066 @Override 067 public int getChildCount() { 068 return 0; 069 } 070 071 @Override 072 public String getShortDescription(Locale locale) { 073 return Bundle.getMessage(locale, "AnalogExpressionConstant_Short"); 074 } 075 076 @Override 077 public String getLongDescription(Locale locale) { 078 NumberFormat numberFormat = NumberFormat.getInstance(locale); 079 return Bundle.getMessage(locale, "AnalogExpressionConstant_Long", numberFormat.format(_value)); 080 } 081 082 /** {@inheritDoc} */ 083 @Override 084 public void setup() { 085 // Do nothing 086 } 087 088 /** {@inheritDoc} */ 089 @Override 090 public void registerListenersForThisClass() { 091 // This class does not have any listeners registered, but we still don't 092 // want a caller to change the value then listeners are registered. 093 // So we set this property to warn the caller when the caller is using 094 // the class in the wrong way. 095 _listenersAreRegistered = true; 096 } 097 098 /** {@inheritDoc} */ 099 @Override 100 public void unregisterListenersForThisClass() { 101 _listenersAreRegistered = false; 102 } 103 104 /** {@inheritDoc} */ 105 @Override 106 public void disposeMe() { 107 } 108 109 110 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AnalogExpressionConstant.class); 111 112}