001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package javax.mail;
021    
022    /**
023     * A representation of a Quota item for a given quota root.
024     *
025     * @version $Rev: 578802 $ $Date: 2007-09-24 09:16:44 -0400 (Mon, 24 Sep 2007) $
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 no 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         * currently 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    }