Source for gnu.inet.imap.Quota

   1: /*
   2:  * Quota.java
   3:  * Copyright (C) 2004 The Free Software Foundation
   4:  * 
   5:  * This file is part of GNU inetlib, a library.
   6:  * 
   7:  * GNU inetlib is free software; you can redistribute it and/or modify
   8:  * it under the terms of the GNU General Public License as published by
   9:  * the Free Software Foundation; either version 2 of the License, or
  10:  * (at your option) any later version.
  11:  * 
  12:  * GNU inetlib is distributed in the hope that it will be useful,
  13:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15:  * GNU General Public License for more details.
  16:  * 
  17:  * You should have received a copy of the GNU General Public License
  18:  * along with this library; if not, write to the Free Software
  19:  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20:  * 
  21:  * Linking this library statically or dynamically with other modules is
  22:  * making a combined work based on this library.  Thus, the terms and
  23:  * conditions of the GNU General Public License cover the whole
  24:  * combination.
  25:  *
  26:  * As a special exception, the copyright holders of this library give you
  27:  * permission to link this library with independent modules to produce an
  28:  * executable, regardless of the license terms of these independent
  29:  * modules, and to copy and distribute the resulting executable under
  30:  * terms of your choice, provided that you also meet, for each linked
  31:  * independent module, the terms and conditions of the license of that
  32:  * module.  An independent module is a module which is not derived from
  33:  * or based on this library.  If you modify this library, you may extend
  34:  * this exception to your version of the library, but you are not
  35:  * obliged to do so.  If you do not wish to do so, delete this
  36:  * exception statement from your version.
  37:  */
  38: 
  39: package gnu.inet.imap;
  40: 
  41: import java.util.ArrayList;
  42: import java.util.List;
  43: import java.util.Map;
  44: import java.util.TreeMap;
  45: 
  46: /**
  47:  * An IMAP quota entry.
  48:  *
  49:  * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  50:  */
  51: public class Quota
  52: {
  53: 
  54:   /**
  55:    * A quota resource entry.
  56:    */
  57:   public static class Resource
  58:   {
  59: 
  60:     String name;
  61:     int current;
  62:     int limit;
  63: 
  64:     /**
  65:      * Specifies a new resource with the given name and limit.
  66:      * @param name the resource name
  67:      * @param limit the resource limit
  68:      */
  69:     public Resource(String name, int limit)
  70:     {
  71:       this(name, -1, limit);
  72:     }
  73: 
  74:     Resource(String name, int current, int limit)
  75:     {
  76:       this.name = name;
  77:       this.current = current;
  78:       this.limit = limit;
  79:     }
  80: 
  81:     /**
  82:      * Returns the resource name.
  83:      */
  84:     public String getName()
  85:     {
  86:       return name;
  87:     }
  88: 
  89:     /**
  90:      * Returns the current usage of the resource, or <code>-1</code> if not
  91:      * known.
  92:      */
  93:     public int getCurrentUsage()
  94:     {
  95:       return current;
  96:     }
  97: 
  98:     /**
  99:      * Returns the resource limit.
 100:      */
 101:     public int getLimit()
 102:     {
 103:       return limit;
 104:     }
 105: 
 106:     /**
 107:      * Debugging.
 108:      */
 109:     public String toString()
 110:     {
 111:       StringBuffer buf = new StringBuffer();
 112:       buf.append('(');
 113:       buf.append(name);
 114:       if (current >= 0)
 115:         {
 116:           buf.append(' ');
 117:           buf.append(current);
 118:         }
 119:       buf.append(' ');
 120:       buf.append(limit);
 121:       buf.append(')');
 122:       return buf.toString();
 123:     }
 124:     
 125:   }
 126: 
 127:   String quotaRoot;
 128:   List resources;
 129: 
 130:   Quota(String text)
 131:   {
 132:     int len = text.length();
 133:     List acc = new ArrayList();
 134:     Namespaces.parse(text, 0, len, acc);
 135: 
 136:     quotaRoot = (String) acc.get(0);
 137:     resources = new ArrayList();
 138:     len = acc.size();
 139:     for (int i = 1; i < len; i++)
 140:       {
 141:         resources.add(parseResource((List) acc.get(i)));
 142:       }
 143:   }
 144: 
 145:   private Resource parseResource(List triple)
 146:   {
 147:     String name = (String) triple.get(0);
 148:     String current = (String) triple.get(1);
 149:     String limit = (String) triple.get(2);
 150:     return new Resource(name,
 151:                         Integer.parseInt(current),
 152:                         Integer.parseInt(limit));
 153:   }
 154: 
 155:   /**
 156:    * Returns the quota root. All mailboxes that share the same named
 157:    * quota root share the resource limits of the quota root.
 158:    */
 159:   public String getQuotaRoot()
 160:   {
 161:     return quotaRoot;
 162:   }
 163: 
 164:   /**
 165:    * Returns the list of quota resources.
 166:    */
 167:   public Resource[] getResources()
 168:   {
 169:     Resource[] ret = new Resource[resources.size()];
 170:     resources.toArray(ret);
 171:     return ret;
 172:   }
 173: 
 174:   /**
 175:    * Debugging.
 176:    */
 177:   public String toString()
 178:   {
 179:     StringBuffer buf = new StringBuffer();
 180:     buf.append(IMAPConnection.quote(UTF7imap.encode(quotaRoot)));
 181:     int len = resources.size();
 182:     for (int i = 0; i < len; i++)
 183:       {
 184:         buf.append(' ');
 185:         buf.append(resources.get(i));
 186:       }
 187:     return buf.toString();
 188:   }
 189:   
 190: }