SquirrelJME/SquirrelJME

View on GitHub
emulators/springcoat-vm/src/main/java/cc/squirreljme/vm/springcoat/SpringArrayObjectDouble.java

Summary

Maintainability
A
1 hr
Test Coverage
// -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
// ---------------------------------------------------------------------------
// SquirrelJME
//     Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
// ---------------------------------------------------------------------------
// SquirrelJME is under the Mozilla Public License Version 2.0.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------

package cc.squirreljme.vm.springcoat;

import cc.squirreljme.vm.springcoat.exceptions.SpringArrayIndexOutOfBoundsException;
import cc.squirreljme.vm.springcoat.exceptions.SpringArrayStoreException;
import cc.squirreljme.vm.springcoat.exceptions.SpringNegativeArraySizeException;

/**
 * Array backed by a double array.
 *
 * @since 2018/11/14
 */
public final class SpringArrayObjectDouble
    extends SpringArrayObject
{
    /** Elements in the array. */
    private final double[] _elements;
    
    /**
     * Initializes the array.
     *
     * @param __self The self type.
     * @param __l The array length.
     * @throws NullPointerException On null arguments.
     * @throws SpringNegativeArraySizeException If the array size is negative.
     * @since 2018/11/14
     */
    public SpringArrayObjectDouble(SpringClass __self, int __l)
        throws NullPointerException
    {
        super(__self, __l);
        
        // Initialize elements
        this._elements = new double[__l];
    }
    
    /**
     * Wraps the native array.
     *
     * @param __self The self type.
     * @param __a The array to wrap.
     * @throws NullPointerException On null arguments.
     * @since 2018/11/18
     */
    public SpringArrayObjectDouble(SpringClass __self, double[] __a)
        throws NullPointerException
    {
        super(__self, __a.length);
        
        this._elements = __a;
    }
    
    /**
     * {@inheritDoc}
     * @since 2018/11/19
     */
    @Override
    public final double[] array()
    {
        return this._elements;
    }
    
    /**
     * {@inheritDoc}
     * @since 2018/11/14
     */
    @Override
    @SuppressWarnings({"unchecked"})
    public final <C> C get(Class<C> __cl, int __dx)
        throws NullPointerException, SpringArrayIndexOutOfBoundsException
    {
        // Read value
        try
        {
            return (C)Double.valueOf(this._elements[__dx]);
        }
        
        /* {@squirreljme.error BK0b Out of bounds access to array. (The index;
        The length of the array)} */
        catch (IndexOutOfBoundsException e)
        {
            throw new SpringArrayIndexOutOfBoundsException(
                String.format("BK0b %d %d", __dx, this.length), e);
        }
    }
    
    /**
     * {@inheritDoc}
     * @since 2018/11/14
     */
    @Override
    public final void set(int __dx, Object __v)
        throws SpringArrayStoreException, SpringArrayIndexOutOfBoundsException
    {
        // Try setting
        try
        {
            this._elements[__dx] = ((Double)__v).doubleValue();
        }
        
        /* {@squirreljme.error BK0c Could not set the index in the double
        array.} */
        catch (ClassCastException e)
        {
            throw new SpringArrayStoreException("BK0c", e);
        }
        
        /* {@squirreljme.error BK0d Out of bounds access to array. (The index;
        The length of the array)} */
        catch (IndexOutOfBoundsException e)
        {
            throw new SpringArrayIndexOutOfBoundsException(
                String.format("BK0d %d %d", __dx, this.length), e);
        }
    }
}