You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigTarget.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.config;
  23. import java.io.Serializable;
  24. import javax.annotation.Nonnull;
  25. /**
  26. * Represents the target of a particular config source.
  27. * <p>
  28. * Note: this class has a natural ordering that is inconsistent with equals.
  29. */
  30. public class ConfigTarget implements Comparable<ConfigTarget>, Serializable {
  31. /** The possible target types. */
  32. public enum TYPE {
  33. /** Client-wide default settings. */
  34. GLOBALDEFAULT,
  35. /** Client-wide settings. */
  36. GLOBAL,
  37. /** Settings for a theme. */
  38. THEME,
  39. /** Settings for an IRCd. */
  40. IRCD,
  41. /** Settings for a network. */
  42. NETWORK,
  43. /** Settings for a server. */
  44. SERVER,
  45. /** Settings for a channel. */
  46. CHANNEL,
  47. /** Settings for a protocol (parser). */
  48. PROTOCOL,
  49. /** A custom identity, which doesn't contain settings to be loaded. */
  50. CUSTOM,
  51. }
  52. /** A version number for this class. */
  53. private static final long serialVersionUID = 2;
  54. /** The type of this target. */
  55. protected TYPE type = ConfigTarget.TYPE.GLOBAL;
  56. /** The data of this target. */
  57. protected String data;
  58. /** The user-defined ordering for this target. */
  59. protected int order = 50000;
  60. /**
  61. * Sets the ordering value for this target. Lower means higher preference.
  62. *
  63. * @param order The new order to use
  64. */
  65. public void setOrder(final int order) {
  66. this.order = order;
  67. }
  68. /**
  69. * Retrieves the ordering value for this target. Lower means higher preference.
  70. *
  71. * @return This target's order
  72. */
  73. public int getOrder() {
  74. return order;
  75. }
  76. /** Sets this target to be a global config source. */
  77. public void setGlobal() {
  78. type = TYPE.GLOBAL;
  79. data = "";
  80. }
  81. /** Sets this target to be a global default source. */
  82. public void setGlobalDefault() {
  83. type = TYPE.GLOBALDEFAULT;
  84. data = "";
  85. }
  86. /** Sets this target to be a theme source. */
  87. public void setTheme() {
  88. type = TYPE.THEME;
  89. data = "";
  90. }
  91. /**
  92. * Sets this target to be a custom identity.
  93. *
  94. * @param customType The type of custom identity
  95. *
  96. * @since 0.6.4
  97. */
  98. public void setCustom(final String customType) {
  99. type = TYPE.CUSTOM;
  100. data = customType;
  101. }
  102. /**
  103. * Determines if this target is the specified custom type.
  104. *
  105. * @param customType The type of custom identity
  106. *
  107. * @return True if this target is a CUSTOM type with the specified type.
  108. *
  109. * @since 0.6.4
  110. */
  111. public boolean isCustom(final String customType) {
  112. return type == TYPE.CUSTOM && customType.equals(data);
  113. }
  114. /**
  115. * Sets this target to target an ircd.
  116. *
  117. * @param ircd The ircd to target
  118. */
  119. public void setIrcd(final String ircd) {
  120. type = TYPE.IRCD;
  121. data = ircd;
  122. }
  123. /**
  124. * Sets this target to target a network.
  125. *
  126. * @param network The network to target
  127. */
  128. public void setNetwork(final String network) {
  129. type = TYPE.NETWORK;
  130. data = network;
  131. }
  132. /**
  133. * Sets this target to target a server.
  134. *
  135. * @param server The server to target
  136. */
  137. public void setServer(final String server) {
  138. type = TYPE.SERVER;
  139. data = server;
  140. }
  141. /**
  142. * Sets this target to target a channel.
  143. *
  144. * @param channel The channel to target, in the form of channel@network
  145. */
  146. public void setChannel(final String channel) {
  147. type = TYPE.CHANNEL;
  148. data = channel;
  149. }
  150. /**
  151. * Sets this target to target a protocol.
  152. *
  153. * @param protocol The protocol to target
  154. *
  155. * @since 0.6.3
  156. */
  157. public void setProtocol(final String protocol) {
  158. type = TYPE.PROTOCOL;
  159. data = protocol;
  160. }
  161. /**
  162. * Retrieves the type of this target.
  163. *
  164. * @return This target's type
  165. */
  166. public TYPE getType() {
  167. return type;
  168. }
  169. /**
  170. * Returns a string representation of the type of this target.
  171. *
  172. * @return A string describing this target's type
  173. */
  174. public String getTypeName() {
  175. return type.toString().toLowerCase();
  176. }
  177. /**
  178. * Retrieves the data associated with this target.
  179. *
  180. * @return This target's data
  181. */
  182. public String getData() {
  183. return data;
  184. }
  185. @Override
  186. public int hashCode() {
  187. return type.ordinal() + data.hashCode();
  188. }
  189. @Override
  190. public boolean equals(final Object obj) {
  191. return obj instanceof ConfigTarget
  192. && type == ((ConfigTarget) obj).getType()
  193. && data.equals(((ConfigTarget) obj).getData());
  194. }
  195. /**
  196. * Compares this target to another to determine which is more specific.
  197. *
  198. * @param target The target to compare to
  199. *
  200. * @return a negative integer if this config is less specific, 0 if they're equal, or a positive
  201. * integer if this is more specific
  202. */
  203. @Override
  204. public int compareTo(@Nonnull final ConfigTarget target) {
  205. if (type == target.getType()) {
  206. return target.getOrder() - order;
  207. } else {
  208. return type.compareTo(target.getType());
  209. }
  210. }
  211. /**
  212. * Returns a string representation of this object.
  213. *
  214. * @return A string representation of this object
  215. */
  216. @Override
  217. public String toString() {
  218. switch (type) {
  219. case GLOBALDEFAULT:
  220. return "Global defaults";
  221. case THEME:
  222. return "Theme";
  223. case CUSTOM:
  224. return "Custom: " + data;
  225. case IRCD:
  226. return "Ircd specific: " + data;
  227. case NETWORK:
  228. return "Network specific: " + data;
  229. case SERVER:
  230. return "Server specific: " + data;
  231. case CHANNEL:
  232. return "Channel specific: " + data;
  233. case PROTOCOL:
  234. return "Protocol specific: " + data;
  235. default:
  236. return "Global config";
  237. }
  238. }
  239. }