Project

General

Profile

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

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

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

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

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

    
39
  private static final ImplementedTablebasesList[] objects;
40

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

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

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

    
59
    return false;
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

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

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

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

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

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

    
126
    return null;
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

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