Project

General

Profile

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

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

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.ObjectSignatures;
15

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

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

    
21
public enum ImplementedTablebasesList
22
{
23
  PYRAMINX_DUO  (ObjectSignatures.PDUO_2, TablebasesPyraminxDuo.class),
24
  IVY_CUBE      (ObjectSignatures.IVY_2 , TablebasesIvyCube.class),
25
  CU_232        (ObjectSignatures.CU_232, TablebasesCuboid232.class),
26
  PYRA_3        (ObjectSignatures.PYRA_3, TablebasesPyraminx.class),
27
  DIAMOND       (ObjectSignatures.DIAM_2, TablebasesSkewbDiamond.class),
28
  CUBE2         (ObjectSignatures.CUBE_2, TablebasesCube2.class),
29
  ;
30

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

    
33
  private final int mSignature;
34
  private final Class<? extends TablebasesAbstract> mClass;
35

    
36
  private static final ImplementedTablebasesList[] objects;
37

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

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  public static boolean hasTablebase(int signature)
52
    {
53
    for(int i=0; i<NUM_OBJECTS; i++)
54
      if( objects[i].mSignature == signature ) return true;
55

    
56
    return false;
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  ImplementedTablebasesList(int sig, final Class<? extends TablebasesAbstract> clazz)
62
    {
63
    mSignature = sig;
64
    mClass= clazz;
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  public int getSignature()
70
    {
71
    return mSignature;
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  public static TablebasesAbstract createPacked(Resources res, int signature)
77
    {
78
    Class<? extends TablebasesAbstract> clazz=null;
79

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

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

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

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

    
116
    return null;
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  public static TablebasesAbstract createUnpacked(int signatures)
122
    {
123
    Class<? extends TablebasesAbstract> clazz=null;
124

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

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

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

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

    
160
    return null;
161
    }
162
}
(1-1/10)