Project

General

Profile

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

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

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

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

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

    
37
  private static final ImplementedTablebasesList[] objects;
38

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

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

    
57
    return false;
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

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

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

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

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

    
124
    return null;
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  public static TablebasesAbstract createPacked(Resources res, int signature)
130
    {
131
    return create(res,signature,true);
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  public static TablebasesAbstract createUnpacked(int signature)
137
    {
138
    return create(null,signature,false);
139
    }
140
}
(1-1/12)