001/*
002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.debug.internal;
024
025/**
026 * A name and index for a value managed in a thread local value map. All access to the value is made
027 * via a {@link DebugValue} instance.
028 */
029public abstract class DebugValue implements Comparable<DebugValue> {
030
031    private final String name;
032    private int index;
033    private boolean conditional;
034
035    protected DebugValue(String name, boolean conditional) {
036        this.name = name;
037        this.index = -1;
038        this.conditional = conditional;
039    }
040
041    public long getCurrentValue() {
042        ensureInitialized();
043        return DebugScope.getInstance().getCurrentValue(index);
044    }
045
046    protected void setCurrentValue(long l) {
047        ensureInitialized();
048        DebugScope.getInstance().setCurrentValue(index, l);
049    }
050
051    public void setConditional(boolean flag) {
052        conditional = flag;
053    }
054
055    public boolean isConditional() {
056        return conditional;
057    }
058
059    private void ensureInitialized() {
060        if (index == -1) {
061            index = KeyRegistry.register(this);
062        }
063    }
064
065    protected void addToCurrentValue(long value) {
066        setCurrentValue(getCurrentValue() + value);
067    }
068
069    /**
070     * Gets the globally unique index for the value represented by this object.
071     */
072    public int getIndex() {
073        ensureInitialized();
074        return index;
075    }
076
077    /**
078     * Gets the globally unique name for the value represented by this object.
079     */
080    public String getName() {
081        return name;
082    }
083
084    public int compareTo(DebugValue o) {
085        return name.compareTo(o.name);
086    }
087
088    @Override
089    public String toString() {
090        return name + "@" + index;
091    }
092
093    public abstract String toString(long value);
094}