001    /**
002     *
003     * Copyright 2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package javax.mail;
019    
020    
021    /**
022     * A representation of a Quota item for a given quota root.
023     *
024     * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
025     */
026    
027    public class Quota {
028        /**
029         * The name of the quota root.
030         */
031        public String quotaRoot;
032    
033        /**
034         * The resources associated with this quota root.
035         */
036        public Resource[] resources;
037    
038    
039        /**
040         * Create a Quota with the given name and not resources.
041         *
042         * @param quotaRoot The quota root name.
043         */
044        public Quota(String quotaRoot) {
045            this.quotaRoot = quotaRoot;
046        }
047    
048        /**
049         * Set a limit value for a resource.  If the resource is not
050         * current associated with this Quota, a new Resource item is
051         * added to the resources list.
052         *
053         * @param name   The target resource name.
054         * @param limit  The new limit value for the resource.
055         */
056        public void setResourceLimit(String name, long limit) {
057            Resource target = findResource(name);
058            target.limit = limit;
059        }
060    
061        /**
062         * Locate a particular named resource, adding one to the list
063         * if it does not exist.
064         *
065         * @param name   The target resource name.
066         *
067         * @return A Resource item for this named resource (either existing or new).
068         */
069        private Resource findResource(String name) {
070            // no resources yet?  Make it so.
071            if (resources == null) {
072                Resource target = new Resource(name, 0, 0);
073                resources = new Resource[] { target };
074                return target;
075            }
076    
077            // see if this one exists and return it.
078            for (int i = 0; i < resources.length; i++) {
079                Resource current = resources[i];
080                if (current.name.equalsIgnoreCase(name)) {
081                    return current;
082                }
083            }
084    
085            // have to extend the array...this is a pain.
086            Resource[] newResources = new Resource[resources.length + 1];
087            System.arraycopy(resources, 0, newResources, 0, resources.length);
088            Resource target = new Resource(name, 0, 0);
089            newResources[resources.length] = target;
090            resources = newResources;
091            return target;
092        }
093    
094    
095    
096        /**
097         * A representation of a given resource definition.
098         */
099        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    }