applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / picking / Message.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.client.picking;\r
23 \r
24 /**\r
25  * Abstract base class for all message types supported by the picking API.\r
26  */\r
27 public abstract class Message {\r
28   protected String message;\r
29 \r
30   /**\r
31    * Constructs a new message.\r
32    */\r
33   protected Message() {\r
34 \r
35   }\r
36 \r
37   /**\r
38    * Returns the raw message content as a string.\r
39    * \r
40    * @return the raw message content as a string\r
41    */\r
42   public String getRawMessage() {\r
43     return message;\r
44   }\r
45 \r
46   /**\r
47    * compare the on-wire message content of the given message to this.\r
48    * \r
49    * @param msg\r
50    * @return true if message content is equal\r
51    */\r
52   public boolean equals(Message msg) {\r
53     return message.equals(msg.getRawMessage());\r
54   }\r
55 \r
56   /**\r
57    * Test consistence of a Message class implementation. This method throws an\r
58    * error if the message object cannot be parsed into another instance of the\r
59    * same object by invoking the MessageImpl(String this.getRawMessage())\r
60    * constructor or that newinstance.getRawMessage != this.getRawMessage\r
61    */\r
62   public void validate() {\r
63     try {\r
64       java.lang.reflect.Constructor msgcons = this.getClass().getConstructor(\r
65           new Class[] { String.class });\r
66       if (msgcons == null) {\r
67         throw new Exception("No " + this.getClass().getName()\r
68             + "(String rawmessage) constructor.");\r
69       }\r
70       Message instance = (Message) msgcons.newInstance(new Object[] { this\r
71           .getRawMessage() });\r
72       if (!instance.getRawMessage().equals(getRawMessage())) {\r
73         throw new Error(\r
74             "Raw Message Content does not match :\nInitial Message:"\r
75                 + getRawMessage() + "\nParsed and regnerated as :\n"\r
76                 + instance.getRawMessage() + "\n");\r
77       }\r
78     } catch (Exception e) {\r
79       throw new Error("Message implementation broken for " + this.getClass(), e);\r
80     }\r
81   }\r
82 \r
83 }\r