001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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 */
017package org.apache.activemq.usage;
018
019
020public abstract class PercentLimitUsage <T extends Usage> extends Usage<T> {
021
022    protected int percentLimit = 0;
023
024    /**
025     * @param parent
026     * @param name
027     * @param portion
028     */
029    public PercentLimitUsage(T parent, String name, float portion) {
030        super(parent, name, portion);
031    }
032
033    public void setPercentLimit(int percentLimit) {
034        usageLock.writeLock().lock();
035        try {
036            this.percentLimit = percentLimit;
037            updateLimitBasedOnPercent();
038        } finally {
039            usageLock.writeLock().unlock();
040        }
041    }
042
043    public int getPercentLimit() {
044        usageLock.readLock().lock();
045        try {
046            return percentLimit;
047        } finally {
048            usageLock.readLock().unlock();
049        }
050    }
051
052    protected abstract void updateLimitBasedOnPercent();
053}