Project

General

Profile

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

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

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
  DIAMOND       (ObjectType.DIAM_2, TablebasesSkewbDiamond.class),
28
  ;
29

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

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

    
35
  private static final ImplementedTablebasesList[] objects;
36

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

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
55
    return false;
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

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

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

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

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

    
115
    return null;
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

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

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

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

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

    
159
    return null;
160
    }
161
}
(1-1/9)