Project

General

Profile

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

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

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
  ;
27

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

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

    
33
  private static final ImplementedTablebasesList[] objects;
34

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

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
53
    return false;
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

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

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

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

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

    
113
    return null;
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

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

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

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

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

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

    
157
    return null;
158
    }
159
}
(1-1/7)