Applied-Energistics-2-tiler.../src/main/java/appeng/core/features/registries/GridCacheRegistry.java

87 lines
3 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 - 2014, AlgorithmX2, All rights reserved.
*
* 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-09-24 02:26:27 +02:00
package appeng.core.features.registries;
2014-09-24 02:26:27 +02:00
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
2014-09-24 02:26:27 +02:00
import java.util.HashMap;
import java.util.Map;
2014-09-24 02:26:27 +02:00
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridCache;
import appeng.api.networking.IGridCacheRegistry;
import appeng.core.AELog;
public final class GridCacheRegistry implements IGridCacheRegistry
2014-09-24 02:26:27 +02:00
{
private final Map<Class<? extends IGridCache>, Class<? extends IGridCache>> caches = new HashMap<Class<? extends IGridCache>, Class<? extends IGridCache>>();
2014-09-24 02:26:27 +02:00
@Override
2015-09-30 14:24:40 +02:00
public void registerGridCache( final Class<? extends IGridCache> iface, final Class<? extends IGridCache> implementation )
2014-09-24 02:26:27 +02:00
{
if( iface.isAssignableFrom( implementation ) )
2015-04-29 02:30:53 +02:00
{
2014-12-29 15:13:47 +01:00
this.caches.put( iface, implementation );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
else
2015-04-29 02:30:53 +02:00
{
throw new IllegalArgumentException( "Invalid setup, grid cache must either be the same class, or an interface that the implementation implements. Gotten: " + iface + " and " + implementation );
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
}
@Override
2015-09-30 14:24:40 +02:00
public HashMap<Class<? extends IGridCache>, IGridCache> createCacheInstance( final IGrid g )
2014-09-24 02:26:27 +02:00
{
2015-09-30 14:24:40 +02:00
final HashMap<Class<? extends IGridCache>, IGridCache> map = new HashMap<Class<? extends IGridCache>, IGridCache>();
2014-09-24 02:26:27 +02:00
2015-09-30 14:24:40 +02:00
for( final Class<? extends IGridCache> iface : this.caches.keySet() )
2014-09-24 02:26:27 +02:00
{
try
{
2015-09-30 14:24:40 +02:00
final Constructor<? extends IGridCache> c = this.caches.get( iface ).getConstructor( IGrid.class );
2014-09-24 02:26:27 +02:00
map.put( iface, c.newInstance( g ) );
}
2015-09-30 14:24:40 +02:00
catch( final NoSuchMethodException e )
{
AELog.error( "Grid Caches must have a constructor with IGrid as the single param." );
throw new IllegalArgumentException( e );
}
2015-09-30 14:24:40 +02:00
catch( final InvocationTargetException e )
{
AELog.error( "Grid Caches must have a constructor with IGrid as the single param." );
throw new IllegalStateException( e );
}
2015-09-30 14:24:40 +02:00
catch( final InstantiationException e )
{
AELog.error( "Grid Caches must have a constructor with IGrid as the single param." );
throw new IllegalStateException( e );
}
2015-09-30 14:24:40 +02:00
catch( final IllegalAccessException e )
2014-09-24 02:26:27 +02:00
{
AELog.error( "Grid Caches must have a constructor with IGrid as the single param." );
throw new IllegalStateException( e );
2014-09-24 02:26:27 +02:00
}
}
return map;
}
}