husted      2003/01/14 14:13:37

  Added:       resources/src/java/org/apache/commons/resources/impl
                        BasicMessageList.java BasicMessage.java
  Log:
  Basic implementations of the Message and MessageList interfaces.
  
  Revision  Changes    Path
  1.1                  
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/BasicMessageList.java
  
  Index: BasicMessageList.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/BasicMessageList.java,v
 1.1 2003/01/14 22:13:36 husted Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/14 22:13:36 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project" and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  // ------------------------------------------------------------------------ 78
  
  package org.apache.commons.resources.impl;
  
  import org.apache.commons.resources.MessageList;
  import org.apache.commons.resources.Message;
  
  import java.io.Serializable;
  import java.util.*;
  
  /**
   * A basic implementation of a MessageList.
   * <p>
   * Orginally based on org.apache.http.action.ActionMessages, Revision 1.7.
   *
   * @author David Geary
   * @author Craig R. McClanahan
   * @author David Winterfeldt
   * @author David Graham
   * @author Ted Husted
   * @version $Revision: 1.1 $ $Date: 2003/01/14 22:13:36 $
   */
  public class BasicMessageList implements Serializable, MessageList {
  
      /**
       * The accumulated set of <code>Message</code> objects (represented
       * as an ArrayList) for each property, keyed by property name.
       */
      protected HashMap messages = new HashMap();
  
      /**
       * The current number of the property/key being added.  This is used
       * to maintain the order messages are added.
       */
      private int iCount = 0;
  
      // --------------------------------------------------------- Public Methods
  
      /**
       * Create an empty <code>MessageList</code> object.
       */
      public BasicMessageList() {
          super();
      }
  
      /**
       * Create an <code>MessageList</code> object initialized to use
       * the given value for the "global" message key.
       *
       * @param globalMessageKey The new default global message key
       */
      public BasicMessageList(String globalMessageKey) {
          super();
          setGlobalMessageKey(globalMessageKey);
      }
  
      /**
       * Create an <code>MessageList</code> object initialized with the given
       * messages.
       *
       * @param messages The messages to be initially added to this object.
       */
      public BasicMessageList(MessageList messages) {
          super();
          this.add(messages);
      }
  
      /**
       * Create an <code>MessageList</code> object initialized with the given
       * messages and the given global message key.
       *
       * @param globalMessageKey The new default global message key
       * @param messages The messages to be initially added to this object.
       */
      public BasicMessageList(String globalMessageKey, MessageList messages) {
          super();
          this.add(messages);
      }
  
      /**
       * The "global" message key for this MessageList
       * [GLOBAL_MESSAGE_KEY].
       */
      private String globalMessageKey = GLOBAL_MESSAGE_KEY;
  
      // See interface for JavaDoc
      public String getGlobalMessageKey() {
          return this.globalMessageKey;
      };
  
      // See interface for JavaDoc
      public void setGlobalMessageKey(String globalMessageKey) {
          this.globalMessageKey = globalMessageKey;
      };
  
      // See interface for JavaDoc
      public void add(String property, Message message) {
  
          MessageItem item = (MessageItem) messages.get(property);
          List list = null;
  
          if (item == null) {
              list = new ArrayList();
              item = new MessageItem(list, iCount++);
  
              messages.put(property, item);
          } else {
              list = item.getList();
          }
  
          list.add(message);
  
      }
  
  
      // See interface for JavaDoc
      public void add(Message message) {
  
          add(MessageList.GLOBAL_MESSAGE_KEY, message);
      }
  
  
      // See interface for JavaDoc
      public void add(MessageList messages) {
          // loop over properties
          Iterator props = messages.properties();
          while (props.hasNext()) {
              String property = (String) props.next();
  
              // loop over messages for each property
              Iterator msgs = messages.get(property);
              while (msgs.hasNext()) {
                  Message msg = (Message) msgs.next();
                  this.add(property, msg);
              }
  
          }
      }
  
      // See interface for JavaDoc
      public void clear() {
  
          messages.clear();
  
      }
  
      // See interface for JavaDoc
      public boolean empty() {
          return (this.isEmpty());
      }
  
      // See interface for JavaDoc
      public boolean isEmpty() {
          return (messages.isEmpty());
      }
  
      // See interface for JavaDoc
      public Iterator get() {
  
          if (messages.size() == 0) {
              return (Collections.EMPTY_LIST.iterator());
          }
  
          ArrayList results = new ArrayList();
          ArrayList actionItems = new ArrayList();
  
          for (Iterator i = messages.values().iterator(); i.hasNext();) {
              actionItems.add(i.next());
          }
  
          // Sort MessageItems based on the initial order the
          // property/key was added to MessageList.
          Collections.sort(actionItems, new Comparator() {
              public int compare(Object o1, Object o2) {
                  return ((MessageItem) o1).getOrder() - ((MessageItem) o2).getOrder();
              }
          });
  
          for (Iterator i = actionItems.iterator(); i.hasNext();) {
              MessageItem ami = (MessageItem) i.next();
  
              for (Iterator messages = ami.getList().iterator(); messages.hasNext();) {
                  results.add(messages.next());
              }
          }
  
          return (results.iterator());
  
      }
  
      // See interface for JavaDoc
      public Iterator get(String property) {
  
          MessageItem item = (MessageItem) messages.get(property);
  
          if (item == null) {
              return (Collections.EMPTY_LIST.iterator());
          } else {
              return (item.getList().iterator());
          }
  
      }
  
      // See interface for JavaDoc
      public Iterator properties() {
  
          return (messages.keySet().iterator());
  
      }
  
      // See interface for JavaDoc
      public int size() {
  
          int total = 0;
  
          for (Iterator i = messages.values().iterator(); i.hasNext();) {
              MessageItem ami = (MessageItem) i.next();
              total += ami.getList().size();
          }
  
          return (total);
  
      }
  
      // See interface for JavaDoc
      public int size(String property) {
  
          MessageItem ami = (MessageItem) messages.get(property);
  
          if (ami == null)
              return (0);
          else
              return (ami.getList().size());
  
      }
  
      // See interface for JavaDoc
      protected static class MessageItem implements Serializable {
  
          /**
           * The list of <code>Message</code>s.
           */
          List list = null;
  
          /**
           * The position in the list of messages.
           */
          int iOrder = 0;
  
          public MessageItem(List list, int iOrder) {
              this.list = list;
              this.iOrder = iOrder;
          }
  
          public List getList() {
              return list;
          }
  
          public void setList(List list) {
              this.list = list;
          }
  
          public int getOrder() {
              return iOrder;
          }
  
          public void setOrder(int iOrder) {
              this.iOrder = iOrder;
          }
  
      }
  
  } // end BasicMessageList
  
  
  
  1.1                  
jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/BasicMessage.java
  
  Index: BasicMessage.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/BasicMessage.java,v
 1.1 2003/01/14 22:13:36 husted Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/14 22:13:36 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project" and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  // ------------------------------------------------------------------------ 78
  
  package org.apache.commons.resources.impl;
  
  import org.apache.commons.resources.Message;
  
  import java.io.Serializable;
  
  /**
   * A basic implementation of the Message interface.
   * <p>
   * Orginally based on org.apache.http.action.ActionMessage, Revision 1.5.
   *
   * @author Craig R. McClanahan
   * @author David Winterfeldt
   * @author Ted Husted
   * @version $Revision: 1.1 $ $Date: 2003/01/14 22:13:36 $
   */
  public class BasicMessage implements Serializable, Message {
  
  
  // -------------------------------------------------------------- Constructors
  
  
      /**
       * Construct an action message with no replacement values.
       *
       * @param key Message key for this message
       */
      public BasicMessage(String key) {
  
          this.key = key;
          this.values = null;
  
      }
  
  
      /**
       * Construct an action message with the specified replacement values.
       *
       * @param key Message key for this message
       * @param value0 First replacement value
       */
      public BasicMessage(String key, Object value0) {
  
          this.key = key;
          this.values = new Object[]{value0};
  
      }
  
  
      /**
       * Construct an action message with the specified replacement values.
       *
       * @param key Message key for this message
       * @param value0 First replacement value
       * @param value1 Second replacement value
       */
      public BasicMessage(String key, Object value0, Object value1) {
  
          this.key = key;
          this.values = new Object[]{value0, value1};
  
      }
  
  
      /**
       * Construct an action message with the specified replacement values.
       *
       * @param key Message key for this message
       * @param value0 First replacement value
       * @param value1 Second replacement value
       * @param value2 Third replacement value
       */
      public BasicMessage(String key, Object value0, Object value1,
                          Object value2) {
  
          this.key = key;
          this.values = new Object[]{value0, value1, value2};
  
      }
  
  
      /**
       * Construct an action message with the specified replacement values.
       *
       * @param key Message key for this message
       * @param value0 First replacement value
       * @param value1 Second replacement value
       * @param value2 Third replacement value
       * @param value3 Fourth replacement value
       */
      public BasicMessage(String key, Object value0, Object value1,
                          Object value2, Object value3) {
  
          this.key = key;
          this.values = new Object[]{value0, value1, value2, value3};
      }
  
  
      /**
       * Construct an action message with the specified replacement values.
       *
       * @param key Message key for this message
       * @param values Array of replacement values
       */
      public BasicMessage(String key, Object[] values) {
  
          this.key = key;
          this.values = values;
  
      }
  
  
  // -------------------------------------------------------- Instance Variables
  
  
      /**
       * The message key for this message.
       */
      protected String key = null;
  
  
      /**
       * The replacement values for this mesasge.
       */
      protected Object[] values = null;
  
  
      // --------------------------------------------------------- Public Methods
  
  
      // See interface for JavaDoc
      public String getKey() {
  
          return (this.key);
  
      }
  
  
      // See interface for JavaDoc
      public Object[] getValues() {
  
          return (this.values);
  
      }
  
  
  } // end BasicMessage
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to