package net.ranides.demo.debugger.impl;

import com.sun.jdi.*;
import net.ranides.demo.debugger.DiMirror;

public class CDiMirror implements DiMirror {

    protected final VirtualMachine vm;

    protected CDiMirror(VirtualMachine vm) {
        this.vm = vm;
    }

    public Value asMirror(Object value) {
        if (value instanceof String) {
            return vm.mirrorOf((String) value);
        }
        if (value instanceof Integer) {
            return vm.mirrorOf((Integer) value);
        }
        if (value instanceof Short) {
            return vm.mirrorOf((Short) value);
        }
        if (value instanceof Byte) {
            return vm.mirrorOf((Byte) value);
        }
        if (value instanceof Long) {
            return vm.mirrorOf((Long) value);
        }
        if (value instanceof Float) {
            return vm.mirrorOf((Float) value);
        }
        if (value instanceof Double) {
            return vm.mirrorOf((Double) value);
        }
        if (value instanceof Boolean) {
            return vm.mirrorOf((Boolean) value);
        }
        if (value instanceof Character) {
            return vm.mirrorOf((Character) value);
        }
        throw new IllegalArgumentException("Not supported value: " + value);
    }

    public Object asPrimitive(Value value) {
        if (value instanceof StringReference) {
            return ((StringReference) value).value();
        }
        if (value instanceof IntegerValue) {
            return ((IntegerValue) value).value();
        }
        if (value instanceof ShortValue) {
            return ((ShortValue) value).value();
        }
        if (value instanceof ByteValue) {
            return ((ByteValue) value).value();
        }
        if (value instanceof LongValue) {
            return ((LongValue) value).value();
        }
        if (value instanceof FloatValue) {
            return ((FloatValue) value).value();
        }
        if (value instanceof DoubleValue) {
            return ((DoubleValue) value).value();
        }
        if (value instanceof BooleanValue) {
            return ((BooleanValue) value).value();
        }
        if (value instanceof CharValue) {
            return ((CharValue) value).value();
        }
        throw new IllegalArgumentException("Not supported value: " + value);
    }

}
