1 /*
2  * hunt-proton: AMQP Protocol library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 
13 module hunt.proton.amqp.transport.Role;
14 
15 import hunt.Enum;
16 import  std.concurrency : initOnce;
17 import hunt.Boolean;
18 import hunt.Exceptions;
19 import hunt.util.Common;
20 import hunt.util.Comparator;
21 
22 class Role  {
23 
24   protected string _name;
25 
26     static Role  SENDER() {
27         __gshared Role  inst;
28         return initOnce!inst(new Role("SENDER",0));
29     }
30 
31     static Role  RECEIVER() {
32         __gshared Role  inst;
33         return initOnce!inst(new Role("RECEIVER",1));
34     }
35 
36 
37   this(string name, int ordinal) {
38     this._name = name;
39     this._ordinal = ordinal;
40   }
41 
42   /**
43      * The name of this enum constant, as declared in the enum declaration.
44      * Most programmers should use the {@link #toString} method rather than
45      * accessing this field.
46      */
47 
48 
49   /**
50      * Returns the name of this enum constant, exactly as declared in its
51      * enum declaration.
52      *
53      * <b>Most programmers should use the {@link #toString} method in
54      * preference to this one, as the toString method may return
55      * a more user-friendly name.</b>  This method is designed primarily for
56      * use in specialized situations where correctness depends on getting the
57      * exact name, which will not vary from release to release.
58      *
59      * @return the name of this enum constant
60      */
61   final string name() {
62     return _name;
63   }
64 
65   /**
66      * The ordinal of this enumeration constant (its position
67      * in the enum declaration, where the initial constant is assigned
68      * an ordinal of zero).
69      *
70      * Most programmers will have no use for this field.  It is designed
71      * for use by sophisticated enum-based data structures, such as
72      * {@link java.util.EnumSet} and {@link java.util.EnumMap}.
73      */
74   protected int _ordinal;
75 
76   /**
77      * Returns the ordinal of this enumeration constant (its position
78      * in its enum declaration, where the initial constant is assigned
79      * an ordinal of zero).
80      *
81      * Most programmers will have no use for this method.  It is
82      * designed for use by sophisticated enum-based data structures, such
83      * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
84      *
85      * @return the ordinal of this enumeration constant
86      */
87   final int ordinal() {
88     return _ordinal;
89   }
90 
91   /**
92      * Returns the name of this enum constant, as contained in the
93      * declaration.  This method may be overridden, though it typically
94      * isn't necessary or desirable.  An enum type should override this
95      * method when a more "programmer-friendly" string form exists.
96      *
97      * @return the name of this enum constant
98      */
99   override string toString() {
100     return _name;
101   }
102 
103     public Boolean getValue()
104     {
105         return new Boolean (this.name() == "RECEIVER");
106     }
107 
108     public int getVal()
109     {
110         return this._ordinal;
111     }
112 
113 
114   override int opCmp(Object o) {
115     Role other = cast(Role) o;
116     Role self = this;
117     if (other is null)
118       throw new NullPointerException();
119     return compare(self.ordinal, other.ordinal);
120   }
121 
122     //int opCmp(int o)
123     //{
124     //    return this._val - o;
125     //}
126 
127 
128 
129     // SENDER, RECEIVER
130 }
131 
132 
133