lubomir@281: /** lubomir@281: * Back 2 Browser Bytecode Translator lubomir@281: * Copyright (C) 2012 Jaroslav Tulach lubomir@281: * lubomir@281: * This program is free software: you can redistribute it and/or modify lubomir@281: * it under the terms of the GNU General Public License as published by lubomir@281: * the Free Software Foundation, version 2 of the License. lubomir@281: * lubomir@281: * This program is distributed in the hope that it will be useful, lubomir@281: * but WITHOUT ANY WARRANTY; without even the implied warranty of lubomir@281: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the lubomir@281: * GNU General Public License for more details. lubomir@281: * lubomir@281: * You should have received a copy of the GNU General Public License lubomir@281: * along with this program. Look for COPYING file in the top folder. lubomir@281: * If not, see http://opensource.org/licenses/GPL-2.0. lubomir@281: */ lubomir@281: package org.apidesign.vm4brwsr; lubomir@281: lubomir@281: public final class Variable implements CharSequence { lubomir@281: private static final String STACK_VAR_PREFIX = "st"; lubomir@307: private static final String LOCAL_VAR_PREFIX = "lc"; lubomir@281: lubomir@281: private final String name; lubomir@281: private final int type; lubomir@281: private final int index; lubomir@281: lubomir@281: private static final char[] TYPE_IDS = { 'I', 'L', 'F', 'D', 'A' }; lubomir@281: lubomir@281: private Variable(final String prefix, final int type, final int index) { lubomir@281: this.name = prefix + TYPE_IDS[type] + index; lubomir@281: this.type = type; lubomir@281: this.index = index; lubomir@281: } lubomir@281: lubomir@281: public static Variable getStackVariable( lubomir@281: final int type, final int index) { lubomir@281: // TODO: precreate frequently used variables lubomir@281: return new Variable(STACK_VAR_PREFIX, type, index); lubomir@281: } lubomir@281: lubomir@307: public static Variable getLocalVariable( lubomir@307: final int type, final int index) { lubomir@307: // TODO: precreate frequently used variables lubomir@307: return new Variable(LOCAL_VAR_PREFIX, type, index); lubomir@307: } lubomir@307: lubomir@281: public String getName() { lubomir@281: return name; lubomir@281: } lubomir@281: lubomir@281: public int getType() { lubomir@281: return type; lubomir@281: } lubomir@281: lubomir@281: public int getIndex() { lubomir@281: return index; lubomir@281: } lubomir@281: lubomir@281: public boolean isCategory2() { lubomir@307: return VarType.isCategory2(type); lubomir@281: } lubomir@281: lubomir@281: @Override lubomir@281: public int length() { lubomir@281: return name.length(); lubomir@281: } lubomir@281: lubomir@281: @Override lubomir@281: public char charAt(final int index) { lubomir@281: return name.charAt(index); lubomir@281: } lubomir@281: lubomir@281: @Override lubomir@281: public CharSequence subSequence(final int start, final int end) { lubomir@281: return name.subSequence(start, end); lubomir@281: } lubomir@281: lubomir@281: @Override lubomir@281: public int hashCode() { lubomir@281: return name.hashCode(); lubomir@281: } lubomir@281: lubomir@281: @Override lubomir@281: public boolean equals(final Object other) { lubomir@281: if (this == other) { lubomir@281: return true; lubomir@281: } lubomir@281: if (!(other instanceof Variable)) { lubomir@281: return false; lubomir@281: } lubomir@281: lubomir@281: return name.equals(((Variable) other).name); lubomir@281: } lubomir@281: lubomir@281: @Override lubomir@281: public String toString() { lubomir@281: return name; lubomir@281: } lubomir@281: }