Project

General

Profile

Download (4.89 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / ImplementedTablebasesList.java @ d8003f3a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.tablebases;
11

    
12
import android.content.res.Resources;
13

    
14
import org.distorted.objectlib.main.ObjectType;
15

    
16
import java.lang.reflect.Constructor;
17
import java.lang.reflect.InvocationTargetException;
18

    
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
public enum ImplementedTablebasesList
22
{
23
  PYRAMINX_DUO  (ObjectType.PDUO_2, TablebasesPyraminxDuo.class),
24
  IVY_CUBE      (ObjectType.IVY_2 , TablebasesIvyCube.class),
25
  CU_232        (ObjectType.CU_232, TablebasesCuboid232.class),
26
  PYRA_3        (ObjectType.PYRA_3, TablebasesPyraminx.class),
27
  ;
28

    
29
  public static final int NUM_OBJECTS = values().length;
30

    
31
  private final ObjectType mType;
32
  private final Class<? extends TablebasesAbstract> mClass;
33

    
34
  private static final ImplementedTablebasesList[] objects;
35

    
36
  static
37
    {
38
    objects = new ImplementedTablebasesList[NUM_OBJECTS];
39
    int i=0;
40

    
41
    for(ImplementedTablebasesList object: ImplementedTablebasesList.values())
42
      {
43
      objects[i++] = object;
44
      }
45
    }
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  public static boolean hasTablebase(ObjectType type)
50
    {
51
    for(int i=0; i<NUM_OBJECTS; i++)
52
      if( objects[i].mType == type ) return true;
53

    
54
    return false;
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  ImplementedTablebasesList(ObjectType type, final Class<? extends TablebasesAbstract> clazz)
60
    {
61
    mType = type;
62
    mClass= clazz;
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  public ObjectType getType()
68
    {
69
    return mType;
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  public static TablebasesAbstract createPacked(Resources res, ObjectType type)
75
    {
76
    Class<? extends TablebasesAbstract> clazz=null;
77

    
78
    for(int i=0; i<NUM_OBJECTS; i++)
79
      if( objects[i].mType == type )
80
        {
81
        clazz = objects[i].mClass;
82
        break;
83
        }
84

    
85
    if( clazz==null ) return null;
86

    
87
    try
88
      {
89
      Constructor<?>[] cons = clazz.getConstructors();
90

    
91
      if( cons.length==2 )
92
        {
93
        Object[] parameters = new Object[] { res };
94
        return (TablebasesAbstract)cons[1].newInstance(parameters);
95
        }
96
      else
97
        {
98
        android.util.Log.e("TablebasesList", "ERROR! number of TablebasesAbstract constructors="+cons.length);
99
        }
100
      }
101
    catch(IllegalAccessException iae)
102
      {
103
      android.util.Log.e("TablebasesList", "Illegal Access Exception: "+iae.getMessage());
104
      }
105
    catch(InstantiationException ie)
106
      {
107
      android.util.Log.e("TablebasesList", "Instantiation Exception: "+ie.getMessage());
108
      }
109
    catch(InvocationTargetException ite)
110
      {
111
      android.util.Log.e("TablebasesList", "Invocation Target Exception: "+ite.getMessage());
112
      }
113

    
114
    return null;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public static TablebasesAbstract createUnpacked(ObjectType type)
120
    {
121
    Class<? extends TablebasesAbstract> clazz=null;
122

    
123
    for(int i=0; i<NUM_OBJECTS; i++)
124
      if( objects[i].mType == type )
125
        {
126
        clazz = objects[i].mClass;
127
        break;
128
        }
129

    
130
    if( clazz==null ) return null;
131

    
132
    try
133
      {
134
      Constructor<?>[] cons = clazz.getConstructors();
135

    
136
      if( cons.length==2 )
137
        {
138
        return (TablebasesAbstract)cons[0].newInstance();
139
        }
140
      else
141
        {
142
        android.util.Log.e("TablebasesList", "ERROR! number of TablebasesAbstract constructors="+cons.length);
143
        }
144
      }
145
    catch(IllegalAccessException iae)
146
      {
147
      android.util.Log.e("TablebasesList", "Illegal Access Exception: "+iae.getMessage());
148
      }
149
    catch(InstantiationException ie)
150
      {
151
      android.util.Log.e("TablebasesList", "Instantiation Exception: "+ie.getMessage());
152
      }
153
    catch(InvocationTargetException ite)
154
      {
155
      android.util.Log.e("TablebasesList", "Invocation Target Exception: "+ite.getMessage());
156
      }
157

    
158
    return null;
159
    }
160
}
(1-1/8)