Coverage Report - org.apache.commons.id.serial.LongGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
LongGenerator
90%
18/20
100%
4/4
1.75
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  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 org.apache.commons.id.serial;
 19  
 
 20  
 import org.apache.commons.id.AbstractLongIdentifierGenerator;
 21  
 
 22  
 import java.io.Serializable;
 23  
 
 24  
 /**
 25  
  * <code>LongGenerator</code> is an Identifier Generator
 26  
  * that generates an incrementing number as a Long object.
 27  
  *
 28  
  * <p>If the <code>wrap</code> argument passed to the constructor is set to
 29  
  * <code>true</code>, the sequence will wrap, returning negative values when
 30  
  * {@link Long#MAX_VALUE} reached; otherwise an {@link IllegalStateException}
 31  
  * will be thrown.</p>
 32  
  *
 33  
  * @author Commons-Id team
 34  
  * @version $Id: LongGenerator.java 480488 2006-11-29 08:57:26Z bayard $
 35  
  */
 36  
 public class LongGenerator extends AbstractLongIdentifierGenerator implements Serializable {
 37  
 
 38  
     /**
 39  
      * <code>serialVersionUID</code> is the serializable UID for the binary version of the class.
 40  
      */
 41  
     private static final long serialVersionUID = 20060122L;
 42  
 
 43  
     /** Should the counter wrap. */
 44  
     private boolean wrapping;
 45  
     /** The counter. */
 46  9
     private long count = 0;
 47  
     
 48  
     /**
 49  
      * Constructor.
 50  
      *
 51  
      * @param wrap should the factory wrap when it reaches the maximum
 52  
      *  long value (or throw an exception)
 53  
      * @param initialValue  the initial long value to start at
 54  
      */
 55  
     public LongGenerator(boolean wrap, long initialValue) {
 56  9
         super();
 57  9
         this.wrapping = wrap;
 58  9
         this.count = initialValue;
 59  9
     }
 60  
 
 61  
     /**
 62  
      * Getter for property wrap.
 63  
      *
 64  
      * @return <code>true</code> if this generator is set up to wrap.
 65  
      *
 66  
      */
 67  
     public boolean isWrap() {
 68  4
         return wrapping;
 69  
     }
 70  
 
 71  
     /**
 72  
      * Sets the wrap property.
 73  
      *
 74  
      * @param wrap value for the wrap property
 75  
      *
 76  
      */
 77  
     public void setWrap(boolean wrap) {
 78  0
         this.wrapping = wrap;
 79  0
     }
 80  
     
 81  
     public Long nextLongIdentifier() {
 82  19
         long value = 0;
 83  19
         if (wrapping) {
 84  14
             synchronized (this) {
 85  14
                 value = count++;
 86  14
             }
 87  14
         } else {
 88  5
             synchronized (this) {
 89  5
                 if (count == Long.MAX_VALUE) {
 90  3
                     throw new IllegalStateException
 91  
                     ("The maximum number of identifiers has been reached");
 92  
                 }
 93  2
                 value = count++;
 94  2
             }
 95  
         }
 96  16
         return new Long(value);
 97  
     }
 98  
 }