Applied-Energistics-2-tiler.../src/main/java/appeng/parts/CableBusStorage.java

147 lines
3.1 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
2014-08-27 07:57:37 +02:00
package appeng.parts;
import javax.annotation.Nullable;
2014-08-27 07:57:37 +02:00
import appeng.api.implementations.parts.IPartCable;
import appeng.api.parts.IFacadePart;
import appeng.api.parts.IPart;
2015-06-16 02:44:59 +02:00
import appeng.api.util.AEPartLocation;
2014-08-27 07:57:37 +02:00
2014-08-27 07:57:37 +02:00
/**
* Thin data storage to optimize memory usage for cables.
*/
public class CableBusStorage
{
private IPartCable center;
private IPart[] sides;
private IFacadePart[] facades;
2014-08-27 07:57:37 +02:00
protected IPartCable getCenter()
{
2014-12-29 15:13:47 +01:00
return this.center;
2014-08-27 07:57:37 +02:00
}
protected void setCenter( IPartCable center )
2014-08-27 07:57:37 +02:00
{
this.center = center;
}
2015-06-16 02:44:59 +02:00
protected IPart getSide( AEPartLocation side )
2014-08-27 07:57:37 +02:00
{
int x = side.ordinal();
if( this.sides != null && this.sides.length > x )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
return this.sides[x];
2015-04-29 02:30:53 +02:00
}
2014-08-27 07:57:37 +02:00
return null;
}
2015-06-16 02:44:59 +02:00
protected void setSide( AEPartLocation side, IPart part )
2014-08-27 07:57:37 +02:00
{
int x = side.ordinal();
if( this.sides != null && this.sides.length > x && part == null )
2014-08-27 07:57:37 +02:00
{
2014-12-29 15:13:47 +01:00
this.sides[x] = null;
this.sides = this.shrink( this.sides, true );
2014-08-27 07:57:37 +02:00
}
else if( part != null )
2014-08-27 07:57:37 +02:00
{
2014-12-29 15:13:47 +01:00
this.sides = this.grow( this.sides, x, true );
this.sides[x] = part;
2014-08-27 07:57:37 +02:00
}
}
private <T> T[] shrink( T[] in, boolean parts )
2014-08-27 07:57:37 +02:00
{
int newSize = -1;
for( int x = 0; x < in.length; x++ )
2015-04-29 02:30:53 +02:00
{
if( in[x] != null )
2015-04-29 02:30:53 +02:00
{
newSize = x;
2015-04-29 02:30:53 +02:00
}
}
2014-08-27 07:57:37 +02:00
if( newSize == -1 )
2015-04-29 02:30:53 +02:00
{
return null;
2015-04-29 02:30:53 +02:00
}
2014-08-27 07:57:37 +02:00
newSize++;
if( newSize == in.length )
2015-04-29 02:30:53 +02:00
{
return in;
2015-04-29 02:30:53 +02:00
}
T[] newArray = (T[]) ( parts ? new IPart[newSize] : new IFacadePart[newSize] );
System.arraycopy( in, 0, newArray, 0, newSize );
return newArray;
2014-08-27 07:57:37 +02:00
}
private <T> T[] grow( T[] in, int newValue, boolean parts )
2014-08-27 07:57:37 +02:00
{
if( in != null && in.length > newValue )
2015-04-29 02:30:53 +02:00
{
2014-08-27 07:57:37 +02:00
return in;
2015-04-29 02:30:53 +02:00
}
2014-08-27 07:57:37 +02:00
int newSize = newValue + 1;
2014-08-27 07:57:37 +02:00
T[] newArray = (T[]) ( parts ? new IPart[newSize] : new IFacadePart[newSize] );
if( in != null )
2015-04-29 02:30:53 +02:00
{
2014-08-27 07:57:37 +02:00
System.arraycopy( in, 0, newArray, 0, in.length );
2015-04-29 02:30:53 +02:00
}
2014-08-27 07:57:37 +02:00
return newArray;
}
public IFacadePart getFacade( int x )
2014-08-27 07:57:37 +02:00
{
if( this.facades != null && this.facades.length > x )
2015-04-29 02:30:53 +02:00
{
return this.facades[x];
2015-04-29 02:30:53 +02:00
}
2014-08-27 07:57:37 +02:00
return null;
}
2014-08-27 07:57:37 +02:00
public void setFacade( int x, @Nullable IFacadePart facade )
{
if( this.facades != null && this.facades.length > x && facade == null )
{
this.facades[x] = null;
this.facades = this.shrink( this.facades, false );
}
else
{
this.facades = this.grow( this.facades, x, false );
this.facades[x] = facade;
}
2014-08-27 07:57:37 +02:00
}
}