1 package eu.medsea.mimeutil.detector;
2
3 import java.util.HashMap;
4 import java.util.Map;
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 final class MagicMimeEntryOperation
31 {
32 private static final Map operationID2operation = new HashMap();
33
34 public static final MagicMimeEntryOperation EQUALS = new MagicMimeEntryOperation('=');
35 public static final MagicMimeEntryOperation LESS_THAN = new MagicMimeEntryOperation('<');
36 public static final MagicMimeEntryOperation GREATER_THAN = new MagicMimeEntryOperation('>');
37 public static final MagicMimeEntryOperation AND = new MagicMimeEntryOperation('&');
38 public static final MagicMimeEntryOperation CLEAR = new MagicMimeEntryOperation('^');
39 public static final MagicMimeEntryOperation NEGATED = new MagicMimeEntryOperation('~');
40 public static final MagicMimeEntryOperation ANY = new MagicMimeEntryOperation('x');
41 public static final MagicMimeEntryOperation NOT_EQUALS = new MagicMimeEntryOperation('!');
42
43 public static MagicMimeEntryOperation getOperation(char operationID)
44 {
45 Character operationIDCharacter = new Character(operationID);
46 return (MagicMimeEntryOperation) operationID2operation.get(operationIDCharacter);
47 }
48
49 public static MagicMimeEntryOperation getOperationForStringField(String content)
50 {
51 MagicMimeEntryOperation operation = getOperation(content);
52
53 if (EQUALS.equals(operation) || LESS_THAN.equals(operation) || GREATER_THAN.equals(operation))
54 return operation;
55 else
56 return EQUALS;
57 }
58
59 public static MagicMimeEntryOperation getOperationForNumberField(String content)
60 {
61 return getOperation(content);
62 }
63
64 private static MagicMimeEntryOperation getOperation(String content)
65 {
66 if (content.length() == 0)
67 return EQUALS;
68
69 MagicMimeEntryOperation operation = getOperation(content.charAt(0));
70 if (operation == null)
71 return EQUALS;
72 else
73 return operation;
74 }
75
76 private static void registerOperation(MagicMimeEntryOperation operation) {
77 Character operationIDCharacter = new Character(operation.getOperationID());
78 if (operationID2operation.containsKey(operationIDCharacter))
79 throw new IllegalStateException("Duplicate registration of operation " + operationIDCharacter);
80
81 operationID2operation.put(operationIDCharacter, operation);
82 }
83
84 private final char operationID;
85
86 MagicMimeEntryOperation(char operationID) {
87 this.operationID = operationID;
88
89 registerOperation(this);
90 }
91
92 public int hashCode() {
93 final int prime = 31;
94 int result = 1;
95 result = prime * result + operationID;
96 return result;
97 }
98
99 public boolean equals(Object obj) {
100 if (this == obj) return true;
101 if (obj == null) return false;
102 if (getClass() != obj.getClass()) return false;
103 MagicMimeEntryOperation other = (MagicMimeEntryOperation) obj;
104 return this.operationID == other.operationID;
105 }
106
107 public final char getOperationID() {
108 return operationID;
109 }
110
111 public String toString() {
112 return this.getClass().getName() + '[' + operationID + ']';
113 }
114 }