View Javadoc

1   /**
2    *
3    * Copyright 2006 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package javax.mail;
19  
20  
21  /**
22   * A representation of a Quota item for a given quota root.
23   *
24   * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
25   */
26  
27  public class Quota {
28      /**
29       * The name of the quota root.
30       */
31      public String quotaRoot;
32  
33      /**
34       * The resources associated with this quota root.
35       */
36      public Resource[] resources;
37  
38  
39      /**
40       * Create a Quota with the given name and not resources.
41       *
42       * @param quotaRoot The quota root name.
43       */
44      public Quota(String quotaRoot) {
45          this.quotaRoot = quotaRoot;
46      }
47  
48      /**
49       * Set a limit value for a resource.  If the resource is not
50       * current associated with this Quota, a new Resource item is
51       * added to the resources list.
52       *
53       * @param name   The target resource name.
54       * @param limit  The new limit value for the resource.
55       */
56      public void setResourceLimit(String name, long limit) {
57          Resource target = findResource(name);
58          target.limit = limit;
59      }
60  
61      /**
62       * Locate a particular named resource, adding one to the list
63       * if it does not exist.
64       *
65       * @param name   The target resource name.
66       *
67       * @return A Resource item for this named resource (either existing or new).
68       */
69      private Resource findResource(String name) {
70          // no resources yet?  Make it so.
71          if (resources == null) {
72              Resource target = new Resource(name, 0, 0);
73              resources = new Resource[] { target };
74              return target;
75          }
76  
77          // see if this one exists and return it.
78          for (int i = 0; i < resources.length; i++) {
79              Resource current = resources[i];
80              if (current.name.equalsIgnoreCase(name)) {
81                  return current;
82              }
83          }
84  
85          // have to extend the array...this is a pain.
86          Resource[] newResources = new Resource[resources.length + 1];
87          System.arraycopy(resources, 0, newResources, 0, resources.length);
88          Resource target = new Resource(name, 0, 0);
89          newResources[resources.length] = target;
90          resources = newResources;
91          return target;
92      }
93  
94  
95  
96      /**
97       * A representation of a given resource definition.
98       */
99      public static class Resource {
100         /**
101          * The resource name.
102          */
103         public String name;
104         /**
105          * The current resource usage.
106          */
107         public long usage;
108         /**
109          * The limit value for this resource.
110          */
111         public long limit;
112 
113 
114         /**
115          * Construct a Resource object from the given name and usage/limit
116          * information.
117          *
118          * @param name   The Resource name.
119          * @param usage  The current resource usage.
120          * @param limit  The Resource limit value.
121          */
122         public Resource(String name, long usage, long limit) {
123             this.name = name;
124             this.usage = usage;
125             this.limit = limit;
126         }
127     }
128 }