Project

General

Profile

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

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

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
  PDUO     (ObjectSignatures.PDUO_2, TBPyraminxDuo.class),
24
  IVY      (ObjectSignatures.IVY_2 , TBIvyCube.class),
25
  CU_232   (ObjectSignatures.CU_232, TBCuboid232.class),
26
  PYRA_3   (ObjectSignatures.PYRA_3, TBPyraminx.class),
27
  DIAM2    (ObjectSignatures.DIAM_2, TBSkewbDiamond.class),
28
  CUBE2    (ObjectSignatures.CUBE_2, TBCube2.class),
29
  MIRR2    (ObjectSignatures.MIRR_2, TBCube2.class),
30
  JING2    (ObjectSignatures.JING_2, TBJing.class),
31
  SKEW2    (ObjectSignatures.SKEW_2, TBSkewb.class),
32
  DINO6    (ObjectSignatures.DINO_3, TBDino6.class),
33
  ;
34

    
35
  public static final int NUM_OBJECTS = values().length;
36

    
37
  private final int mSignature;
38
  private final Class<? extends TablebasesAbstract> mClass;
39

    
40
  private static final ImplementedTablebasesList[] objects;
41

    
42
  static
43
    {
44
    objects = new ImplementedTablebasesList[NUM_OBJECTS];
45
    int i=0;
46

    
47
    for(ImplementedTablebasesList object: ImplementedTablebasesList.values())
48
      {
49
      objects[i++] = object;
50
      }
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  public static boolean hasTablebase(int signature)
56
    {
57
    for(int i=0; i<NUM_OBJECTS; i++)
58
      if( objects[i].mSignature == signature ) return true;
59

    
60
    return false;
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  ImplementedTablebasesList(int sig, final Class<? extends TablebasesAbstract> clazz)
66
    {
67
    mSignature = sig;
68
    mClass= clazz;
69
    }
70

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

    
73
  public int getSignature()
74
    {
75
    return mSignature;
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  private static TablebasesAbstract create(Resources res, int signature, boolean packed)
81
    {
82
    Class<? extends TablebasesAbstract> clazz=null;
83

    
84
    for(int i=0; i<NUM_OBJECTS; i++)
85
      if( objects[i].mSignature == signature )
86
        {
87
        clazz = objects[i].mClass;
88
        break;
89
        }
90

    
91
    if( clazz==null ) return null;
92

    
93
    try
94
      {
95
      Constructor<?>[] cons = clazz.getConstructors();
96

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

    
127
    return null;
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  public static TablebasesAbstract createPacked(Resources res, int signature)
133
    {
134
    return create(res,signature,true);
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  public static TablebasesAbstract createUnpacked(int signature)
140
    {
141
    return create(null,signature,false);
142
    }
143
}
(1-1/15)