001    /*
002     *  jDTAUS - DTAUS fileformat.
003     *  Copyright (c) 2005 Christian Schulte <cs@schulte.it>
004     *
005     *  This library is free software; you can redistribute it and/or
006     *  modify it under the terms of the GNU Lesser General Public
007     *  License as published by the Free Software Foundation; either
008     *  version 2.1 of the License, or any later version.
009     *
010     *  This library is distributed in the hope that it will be useful,
011     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     *  Lesser General Public License for more details.
014     *
015     *  You should have received a copy of the GNU Lesser General Public
016     *  License along with this library; if not, write to the Free Software
017     *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018     *
019     */
020    package org.jdtaus.core.text;
021    
022    import java.io.Serializable;
023    import java.rmi.server.UID;
024    import java.util.Comparator;
025    import java.util.Locale;
026    
027    /**
028     * Application message.
029     * <p>Application messages consist of at least the two properties
030     * {@code timestamp} and {@code formatArguments}. The {@code timestamp} property
031     * will be initialized during instantiation to hold the timestamp of instance
032     * creation. Property {@code formatArguments} holds the arguments to use
033     * for formatting message text. It is recommended that subclasses of this class
034     * are declared {@code final} so that calling {@code getClass()} on a message
035     * instance always returns a type uniquely identifying a message type in the
036     * system.</p>
037     *
038     * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
039     * @version $Id: Message.java 2201 2007-03-21 23:59:00Z schulte2005 $
040     *
041     * @see MessageEventSource
042     */
043    public abstract class Message implements Cloneable, Serializable
044    {
045    
046        //--Constants---------------------------------------------------------------
047    
048        /** Comparator for sorting messsages in ascending order of occurence. */
049        public static final Comparator ASCENDING = new Comparator()
050        {
051            public int compare(final Object o1, final Object o2)
052            {
053                if(!(o1 instanceof Message) || !(o2 instanceof Message))
054                {
055                    throw new IllegalArgumentException();
056                }
057    
058                // TODO JDK 1.5 Long.valueOf(long)
059                final Long l1 = new Long(((Message) o1).getTimestamp());
060                final Long l2 = new Long(((Message) o2).getTimestamp());
061    
062                return l1.compareTo(l2);
063            }
064        };
065    
066        /** Comparator for sorting messsages in descending order of occurence. */
067        public static final Comparator DESCENDING = new Comparator()
068        {
069            public int compare(final Object o1, final Object o2)
070            {
071                if(!(o1 instanceof Message) || !(o2 instanceof Message))
072                {
073                    throw new IllegalArgumentException();
074                }
075    
076                // TODO JDK 1.5 Long.valueOf(long)
077                final Long l1 = new Long(((Message) o1).getTimestamp());
078                final Long l2 = new Long(((Message) o2).getTimestamp());
079    
080                return l2.compareTo(l1);
081            }
082        };
083    
084        //---------------------------------------------------------------Constants--
085        //--Constructors------------------------------------------------------------
086    
087        /** Creates a new {@code Message} instance. */
088        public Message()
089        {
090            super();
091            this.timestamp = System.currentTimeMillis();
092            this.uid = new UID();
093        }
094    
095        //------------------------------------------------------------Constructors--
096        //--Message-----------------------------------------------------------------
097    
098        /**
099         * Unique message identifier.
100         * @serial
101         */
102        private UID uid;
103    
104        /**
105         * The timestamp this message got created.
106         * @serial
107         */
108        private long timestamp;
109    
110        /**
111         * Getter for property {@code timestamp}.
112         *
113         * @return the timestamp this message got created.
114         */
115        public final long getTimestamp()
116        {
117            return this.timestamp;
118        }
119    
120        /**
121         * Getter for property {@code formatArguments}.
122         *
123         * @param locale the locale to be used for the arguments to return.
124         *
125         * @return the arguments to use when formatting the message text.
126         */
127        public abstract Object[] getFormatArguments(Locale locale);
128    
129        /**
130         * Gets the formatted message text.
131         *
132         * @param locale the locale to be used for the text to return.
133         */
134        public abstract String getText(Locale locale);
135    
136        /**
137         * Creates a string representing the properties of the instance.
138         *
139         * @return a string representing the properties of the instance.
140         */
141        private String internalString()
142        {
143            return new StringBuffer(500).
144                append("\n\ttimestamp=").append(this.timestamp).
145                append("\n\tuid=").append(this.uid).
146                append("\n\tformatArguments=").
147                append(this.getFormatArguments(Locale.getDefault())).
148                append("\n\ttext=").append(this.getText(Locale.getDefault())).
149                toString();
150    
151        }
152    
153        //-----------------------------------------------------------------Message--
154        //--Object------------------------------------------------------------------
155    
156        /**
157         * Returns a string representation of the object.
158         *
159         * @return a string representation of the object.
160         */
161        public String toString()
162        {
163            return super.toString() + this.internalString();
164        }
165    
166        /**
167         * Returns a hash code value for this object.
168         *
169         * @return a hash code value for this object.
170         */
171        public final int hashCode()
172        {
173            return this.uid.hashCode();
174        }
175    
176        /**
177         * Indicates whether some other object is equal to this one.
178         * <p>Messages internally cary a UID which is created during instantiation.
179         * This UID is used for comparing {@code o} with the instance.</p>
180         *
181         * @param o the reference object with which to compare.
182         *
183         * @return {@code true} if this object is the same as {@code o};
184         * {@code false} otherwise.
185         *
186         * @see UID
187         */
188        public final boolean equals(final Object o)
189        {
190            return o == this || (o instanceof Message &&
191                ((Message) o).uid.equals(this.uid));
192    
193        }
194    
195        /**
196         * Creates and returns a copy of this object.
197         *
198         * @return a clone of this instance.
199         */
200        public Object clone()
201        {
202            try
203            {
204                return super.clone();
205            }
206            catch(CloneNotSupportedException e)
207            {
208                throw new AssertionError(e);
209            }
210        }
211    
212        //------------------------------------------------------------------Object--
213    
214    }