001 /*
002 * Copyright 2012 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018 package com.unboundid.scim.wink;
019
020
021
022 import java.util.concurrent.Semaphore;
023
024
025
026 /**
027 * A semaphore that can be dynamically resized.
028 */
029 public final class AdjustableSemaphore extends Semaphore
030 {
031 private static final long serialVersionUID = -2211345506986838529L;
032
033
034
035 /**
036 * The maximum number of permits provided by this semaphore.
037 */
038 private int maxPermits;
039
040
041
042 /**
043 * Create a new instance of this semaphore.
044 *
045 * @param maxPermits The initial number of permits.
046 */
047 public AdjustableSemaphore(final int maxPermits)
048 {
049 super(maxPermits);
050 this.maxPermits = maxPermits;
051 }
052
053
054
055 /**
056 * Retrieves the maximum number of permits.
057 * @return The maximum number of permits.
058 */
059 public int getMaxPermits()
060 {
061 return maxPermits;
062 }
063
064
065
066 /**
067 * {@inheritDoc}
068 */
069 @Override
070 public int availablePermits()
071 {
072 // Do not return a negative number of permits. This could happen if
073 // there are threads waiting to acquire a permit and the maximum number
074 // of permits is reduced.
075 final int availablePermits = super.availablePermits();
076 return availablePermits >= 0 ? availablePermits : 0;
077 }
078
079
080
081 /**
082 * Set the maximum number of permits.
083 *
084 * @param maxPermits The maximum number of permits. Must be greater than zero.
085 */
086 public synchronized void setMaxPermits(final int maxPermits)
087 {
088 if (maxPermits < 1)
089 {
090 throw new IllegalArgumentException();
091 }
092
093 int delta = maxPermits - this.maxPermits;
094 if (delta == 0)
095 {
096 return;
097 }
098 else if (delta > 0)
099 {
100 release(delta);
101 }
102 else
103 {
104 reducePermits(-delta);
105 }
106
107 this.maxPermits = maxPermits;
108 }
109 }