summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/database/Transitions.java
blob: e45ad6deb02432093e3acd49dafb3a15ddee8120 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.database;

import com.fourisland.fourpuzzle.transition.InTransition;
import com.fourisland.fourpuzzle.transition.MultidirectionalTransition;
import com.fourisland.fourpuzzle.transition.OutTransition;
import com.fourisland.fourpuzzle.transition.SquareTransition;
import com.fourisland.fourpuzzle.transition.Transition;
import com.fourisland.fourpuzzle.transition.TransitionDirection;
import com.fourisland.fourpuzzle.transition.TransitionUnsupportedException;

/**
 *
 * @author hatkirby
 */
public enum Transitions {
    MapExit(TransitionDirection.Out, new SquareTransition(TransitionDirection.Out)),
    MapEnter(TransitionDirection.In, new SquareTransition(TransitionDirection.In));
    
    private final TransitionDirection dir;
    private Transition trans;
    private Transitions(TransitionDirection dir, Transition trans)
    {
        this.dir = dir;
        
        if (isTransitionSupported(dir, trans))
        {
            this.trans = trans;
        } else {
            throw new TransitionUnsupportedException(trans.getClass().getName(), dir);
        }
    }
    
    Transition getValue()
    {
        return trans;
    }
    
    void setValue(Transition trans)
    {
        if (isTransitionSupported(dir, trans))
        {
            this.trans = trans;
        } else {
            throw new TransitionUnsupportedException(trans.getClass().getName(), dir);
        }
    }
    
    private boolean isTransitionSupported(TransitionDirection dir, Transition trans)
    {
        if (trans instanceof MultidirectionalTransition)
        {
            return (((MultidirectionalTransition) trans).getDirection() == dir);
        } else if (trans instanceof OutTransition)
        {
            return (dir == TransitionDirection.Out);
        } else if (trans instanceof InTransition)
        {
            return (dir == TransitionDirection.In);
        }
        
        return false;
    }
}