/* PseudoCode Interpreted Language (PCIL): Part of the algoviz@vt collection of algorithm visualizations. Copyright (C) 2008 Brandon Malone, Frank Hadlock This file is part of the PseudoCode Interpreted Language. PseudoCode Interpreted Language is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PseudoCode Interpreted Language is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with PseudoCode Interpreted Language. If not, see . */ /* * PrimitiveInteger.java * * Created on September 17, 2007, 9:35 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package dynamicmvc; import java.awt.Component; import java.awt.Dimension; import java.util.ArrayList; import java.util.HashMap; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; /** * * @author Compaq_Owner */ public class PrimitiveInteger extends Primitive { public static String pseudocodeTypeName() { return "Integer"; } public String getType() { return pseudocodeTypeName(); } // private int value; private String name; // // public boolean isNullable() { return (value == 0); } public Primitive copy() { PrimitiveInteger i = new PrimitiveInteger(name, value); return i; } public Primitive deepCopy(HashMap alreadyCopiedAddresses) throws DynamicMVCException { PrimitivePointer p = getPointer(); if (alreadyCopiedAddresses.containsKey(Integer.valueOf(p.getAddress()))) { return PrimitiveFactory.getFromMemory(alreadyCopiedAddresses.get(p.getAddress())); } PrimitivePointer pp = PrimitiveFactory.create(pseudocodeTypeName(), name); alreadyCopiedAddresses.put(p.getAddress(), pp.getAddress()); PrimitiveInteger i = (PrimitiveInteger)pp.dereference(); i.setName(name); i.setValue(value); super.deepCopyDefinedProperties(i, alreadyCopiedAddresses); return i; } public boolean deepEquals(Primitive p) throws DynamicMVCException { if (!(p instanceof PrimitiveInteger)) { DynamicController.errorMessage = "Expecting Integer, but found " + p.getType(); return false; } PrimitiveInteger i = (PrimitiveInteger)p; if(value != i.getValue()) { DynamicController.errorMessage = "Wrong integer value. Expecting '" + value + "', but found '" + i.getValue() + "'."; return false; } return super.deepEqualsDefinedProperties(p); } public void set(Primitive other) throws DynamicMVCException { // if other is an integer, copy its values if (other instanceof PrimitiveInteger) { PrimitiveInteger o = (PrimitiveInteger)other; //setName(o.getName()); setValue(o.getValue()); } else { // otherwise, throw an DynamicMVCException throw new DynamicMVCException("Invalid assignment."); } } /** Creates a new instance of PrimitiveInteger */ public PrimitiveInteger(String name, int value) { super(); setName(name); setValue(value); } public PrimitiveInteger(int value) { super(); setName(""); setValue(value); } public PrimitiveInteger() { super(); setName(""); setValue(0); } // // public int getValue() { return value; } public void setValue(int value) { this.value = value; } public void setValue(String value) { int val = Integer.valueOf(value).intValue(); setValue(val); } public String getName() { return name; } public void setName(String name) { this.name = name; } // // public void populateFromEditComponentWrapper(JComponent editComponentWrapper) { // find the value from the text box Component comp = View.getComponent(editComponentWrapper, getEditComponentName()); if (comp instanceof JTextArea) { JTextArea txtInput = (JTextArea)comp; try { // update the variable setValue(txtInput.getText().trim()); } catch(Exception e) { setValue(0); } } } public String getEditComponentWrapperName() { return "pnl" + name; } public String getEditComponentName() { return "txt" + name; } public JComponent getEditComponent() { // wrap everything in a panel JPanel wrapper = new JPanel(); wrapper.setName(getEditComponentWrapperName()); // create a text box to input the number JTextArea input = new JTextArea(); input.setName(getEditComponentName()); input.setText(String.valueOf(value)); input.setPreferredSize(new Dimension(35, 20)); wrapper.add(input); return wrapper; } public JComponent getReadOnlyComponent() { // wrap everything in a panel JPanel wrapper = new JPanel(); // create a label to display the number JLabel input = new JLabel(); input.setName(getEditComponentName()); input.setText(String.valueOf(value)); wrapper.add(input); return wrapper; } // public Object execute(String methodName, ArrayList arguments) throws DynamicMVCException { // is this a method of mine? if (methodName.equalsIgnoreCase("equals")) { return equals(arguments); } else if (methodName.equalsIgnoreCase("notEqual")) { return !equals(arguments); } else if (methodName.equalsIgnoreCase("assign")) { assign(arguments); return null; } else if (methodName.equalsIgnoreCase("mod")) { return mod(arguments); } else { throw new DynamicMVCException("Invalid method name:'" + methodName + "' on object '" + name + "'."); } } public int compareTo(Object o) throws DynamicMVCException { if (o instanceof PrimitiveInteger) { PrimitiveInteger i = (PrimitiveInteger)o; return compareTo(i); } else if (o instanceof PrimitivePointer) { Primitive i = ((PrimitivePointer)o).dereference(); if (i instanceof PrimitiveInteger) { return compareTo((PrimitiveInteger)i); } else if (i instanceof PrimitiveNull) { return compareTo(Integer.MIN_VALUE); } } else if (o instanceof PrimitiveNull) { return compareTo(Integer.MIN_VALUE); } throw new DynamicMVCException("Invalid comparison"); } public int compareTo(int i) { return value - i; } public boolean equals(ArrayList arguments) throws DynamicMVCException { if (arguments.size() == 1) { PrimitiveInteger i = (PrimitiveInteger)arguments.get(0).dereference(); return equals(i); } throw new DynamicMVCException("Wrong number of arguments."); } public boolean equals(PrimitiveInteger i) { if(i.hasDefinedProperties(this.getDefinedProperties())) { return (value == i.getValue()); } else { return false; } } public boolean equals(Primitive p) { if (p instanceof PrimitiveInteger) { return equals((PrimitiveInteger)p); } return false; } public int compareTo(ArrayList arguments) throws DynamicMVCException { if (arguments.size() == 1) { PrimitiveInteger i = (PrimitiveInteger)arguments.get(0).dereference(); return compareTo(i); } throw new DynamicMVCException("Wrong number of arguments."); } public int compareTo(PrimitiveInteger i) { Integer myInt = new Integer(value); Integer otherInt = new Integer(i.getValue()); return myInt.compareTo(otherInt); } public int compareTo(Primitive p) throws DynamicMVCException { return compareTo((Object)p); } public PrimitivePointer add(PrimitivePointer other) { PrimitiveInteger o = (PrimitiveInteger)other.dereference(); PrimitivePointer ret = PrimitiveFactory.create(PrimitiveInteger.pseudocodeTypeName(), ""); int total = value + o.getValue(); PrimitiveInteger i = (PrimitiveInteger)ret.dereference(); i.setValue(total); return ret; } public PrimitivePointer subtract(PrimitivePointer other) { PrimitiveInteger o = (PrimitiveInteger)other.dereference(); PrimitivePointer ret = PrimitiveFactory.create(PrimitiveInteger.pseudocodeTypeName(), ""); int total = value - o.getValue(); PrimitiveInteger i = (PrimitiveInteger)ret.dereference(); i.setValue(total); return ret; } public PrimitivePointer multiply(PrimitivePointer other) { PrimitiveInteger o = (PrimitiveInteger)other.dereference(); PrimitivePointer ret = PrimitiveFactory.create(PrimitiveInteger.pseudocodeTypeName(), ""); int total = value * o.getValue(); PrimitiveInteger i = (PrimitiveInteger)ret.dereference(); i.setValue(total); return ret; } public PrimitivePointer divide(PrimitivePointer other) { PrimitiveInteger o = (PrimitiveInteger)other.dereference(); PrimitivePointer ret = PrimitiveFactory.create(PrimitiveInteger.pseudocodeTypeName(), ""); int total = value / o.getValue(); PrimitiveInteger i = (PrimitiveInteger)ret.dereference(); i.setValue(total); return ret; } public PrimitivePointer mod(ArrayList arguments) throws DynamicMVCException { if (arguments.size() == 1) { PrimitivePointer i = arguments.get(0); return mod(i); } throw new DynamicMVCException("Wrong number of arguments."); } public PrimitivePointer mod(PrimitivePointer other) { PrimitiveInteger o = (PrimitiveInteger)other.dereference(); PrimitivePointer ret = PrimitiveFactory.create(PrimitiveInteger.pseudocodeTypeName(), ""); int total = value % o.getValue(); PrimitiveInteger i = (PrimitiveInteger)ret.dereference(); i.setValue(total); return ret; } public void assign(ArrayList arguments) throws DynamicMVCException { if (arguments.size() == 1) { PrimitiveInteger i = (PrimitiveInteger)arguments.get(0).dereference(); assign(i); } throw new DynamicMVCException("Wrong number of arguments."); } public void assign(PrimitiveInteger i) { String otherName = i.getName(); int otherValue = i.getValue(); setName(otherName); setValue(otherValue); } public String toString() { return getName() + ": " + getValue(); } public PrimitiveString toPrimitiveString() { PrimitiveString s = (PrimitiveString)(PrimitiveFactory.create(PrimitiveString.pseudocodeTypeName(), "s").dereference()); s.setValue(String.valueOf(getValue())); return s; } public static PrimitivePointer getConstant(int i) { PrimitivePointer p = PrimitiveFactory.create(PrimitiveInteger.pseudocodeTypeName(), ""); ((PrimitiveInteger)p.dereference()).setValue(i); return p; } }