| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SeenString |
|
| 1.0;1 |
| 1 | /* | |
| 2 | * #%L | |
| 3 | * SeenString.java - mongodb-async-driver - Allanbank Consulting, Inc. | |
| 4 | * %% | |
| 5 | * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc. | |
| 6 | * %% | |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 8 | * you may not use this file except in compliance with the License. | |
| 9 | * You may obtain a copy of the License at | |
| 10 | * | |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 12 | * | |
| 13 | * Unless required by applicable law or agreed to in writing, software | |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 16 | * See the License for the specific language governing permissions and | |
| 17 | * limitations under the License. | |
| 18 | * #L% | |
| 19 | */ | |
| 20 | package com.allanbank.mongodb.bson.io; | |
| 21 | ||
| 22 | import java.util.Arrays; | |
| 23 | import java.util.concurrent.atomic.AtomicInteger; | |
| 24 | import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; | |
| 25 | ||
| 26 | /** | |
| 27 | * SeenString is a record of the byte[], value and count for each string/byte[] | |
| 28 | * the cache sees. | |
| 29 | * | |
| 30 | * @api.no This class is <b>NOT</b> part of the drivers API. This class may be | |
| 31 | * mutated in incompatible ways between any two releases of the driver. | |
| 32 | * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved | |
| 33 | */ | |
| 34 | class SeenString { | |
| 35 | /** | |
| 36 | * Atomic updated for all of the SeenString instances to cut down on the | |
| 37 | * number of {@link AtomicInteger} entries required. | |
| 38 | */ | |
| 39 | 1 | private final static AtomicIntegerFieldUpdater<SeenString> ourCountUpdater = AtomicIntegerFieldUpdater |
| 40 | .newUpdater(SeenString.class, "myCount"); | |
| 41 | ||
| 42 | /** The encoded bytes for the seen string. */ | |
| 43 | private final byte[] myBytes; | |
| 44 | ||
| 45 | /** The number of times the string is seen. */ | |
| 46 | private volatile int myCount; | |
| 47 | ||
| 48 | /** The value for the seen string. */ | |
| 49 | private final String myValue; | |
| 50 | ||
| 51 | /** | |
| 52 | * Creates a new SeenString. | |
| 53 | * | |
| 54 | * @param source | |
| 55 | * The source of the bytes in the string. | |
| 56 | * @param offset | |
| 57 | * The offset of the first byte to decode. | |
| 58 | * @param length | |
| 59 | * The length of the bytes to decode with a terminal zero byte. | |
| 60 | * @param decoded | |
| 61 | * The value for the seen string. | |
| 62 | */ | |
| 63 | public SeenString(final byte[] source, final int offset, final int length, | |
| 64 | 136605 | final String decoded) { |
| 65 | 136605 | myBytes = Arrays.copyOfRange(source, offset, offset + length); |
| 66 | 136605 | myValue = decoded; |
| 67 | 136605 | myCount = 0; |
| 68 | 136605 | } |
| 69 | ||
| 70 | /** | |
| 71 | * Returns the encoded bytes for the seen string. | |
| 72 | * | |
| 73 | * @return The encoded bytes for the seen string. | |
| 74 | */ | |
| 75 | public byte[] getBytes() { | |
| 76 | 10282 | return myBytes; |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * Returns the number of times the string is seen. | |
| 81 | * | |
| 82 | * @return The number of times the string is seen. | |
| 83 | */ | |
| 84 | public int getCount() { | |
| 85 | 127893 | return myCount; |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Returns the decoded string for the seen string. | |
| 90 | * | |
| 91 | * @return The decoded string for the seen string. | |
| 92 | */ | |
| 93 | public String getValue() { | |
| 94 | 10458 | return myValue; |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Increments the number of times that the string has been seen. | |
| 99 | */ | |
| 100 | public void incrementCount() { | |
| 101 | 185819 | ourCountUpdater.incrementAndGet(this); |
| 102 | 185821 | } |
| 103 | ||
| 104 | /** | |
| 105 | * Sets the count to zero. | |
| 106 | * | |
| 107 | * @return the previous value for the count. | |
| 108 | */ | |
| 109 | public int reset() { | |
| 110 | 10458 | return ourCountUpdater.getAndSet(this, 0); |
| 111 | } | |
| 112 | } |