1 | /*
|
---|
2 | * 12/01/2008
|
---|
3 | *
|
---|
4 | * AbstractCodeTemplate.java - Base class for code templates.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea.templates;
|
---|
10 |
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * A base class to build code templates on top of.
|
---|
14 | *
|
---|
15 | * @author Robert Futrell
|
---|
16 | * @version 1.0
|
---|
17 | */
|
---|
18 | public abstract class AbstractCodeTemplate implements CodeTemplate {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * The ID of this template.
|
---|
22 | */
|
---|
23 | private String id;
|
---|
24 |
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * This no-arg constructor is required for serialization purposes.
|
---|
28 | */
|
---|
29 | public AbstractCodeTemplate() {
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Creates a new template.
|
---|
35 | *
|
---|
36 | * @param id The ID for this template.
|
---|
37 | * @throws IllegalArgumentException If <code>id</code> is <code>null</code>.
|
---|
38 | */
|
---|
39 | public AbstractCodeTemplate(String id) {
|
---|
40 | setID(id);
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Creates a deep copy of this template.
|
---|
46 | *
|
---|
47 | * @return A deep copy of this template.
|
---|
48 | */
|
---|
49 | public Object clone() {
|
---|
50 | // This method can't be abstract as compilers don't like concrete
|
---|
51 | // subclassses calling super.clone() on an abstract super.
|
---|
52 | try {
|
---|
53 | return super.clone();
|
---|
54 | } catch (CloneNotSupportedException e) {
|
---|
55 | throw new InternalError(
|
---|
56 | "CodeTemplate implementation not Cloneable: " +
|
---|
57 | getClass().getName());
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Compares the <code>StaticCodeTemplate</code> to another.
|
---|
65 | *
|
---|
66 | * @param o Another <code>StaticCodeTemplate</code> object.
|
---|
67 | * @return A negative integer, zero, or a positive integer as this
|
---|
68 | * object is less than, equal-to, or greater than the passed-in
|
---|
69 | * object.
|
---|
70 | * @throws ClassCastException If <code>o</code> is
|
---|
71 | * not an instance of <code>CodeTemplate</code>.
|
---|
72 | */
|
---|
73 | public int compareTo(Object o) {
|
---|
74 | if (!(o instanceof CodeTemplate)) {
|
---|
75 | return -1;
|
---|
76 | }
|
---|
77 | CodeTemplate t2 = (CodeTemplate)o;
|
---|
78 | return getID().compareTo(t2.getID());
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Overridden to return "<code>true</code>" iff {@link #compareTo(Object)}
|
---|
84 | * returns <code>0</code>.
|
---|
85 | *
|
---|
86 | * @return Whether this code template is equal to another.
|
---|
87 | */
|
---|
88 | public boolean equals(Object obj) {
|
---|
89 | if (obj instanceof CodeTemplate) {
|
---|
90 | return compareTo(obj)==0;
|
---|
91 | }
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Returns the ID of this code template.
|
---|
98 | *
|
---|
99 | * @return The template's ID.
|
---|
100 | * @see #setID(String)
|
---|
101 | */
|
---|
102 | public String getID() {
|
---|
103 | return id;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Returns the hash code for this template.
|
---|
109 | *
|
---|
110 | * @return The hash code for this template.
|
---|
111 | */
|
---|
112 | public int hashCode() {
|
---|
113 | return id.hashCode();
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Sets the ID for this template.
|
---|
119 | *
|
---|
120 | * @param id The ID for this template.
|
---|
121 | * @throws IllegalArgumentException If <code>id</code> is <code>null</code>.
|
---|
122 | * @see #getID()
|
---|
123 | */
|
---|
124 | public void setID(String id) {
|
---|
125 | if (id==null) {
|
---|
126 | throw new IllegalArgumentException("id cannot be null");
|
---|
127 | }
|
---|
128 | this.id = id;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | } |
---|