com.unboundid.util.FileValuePatternComponent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unboundid-ldapsdk Show documentation
Show all versions of unboundid-ldapsdk Show documentation
The UnboundID LDAP SDK for Java is a fast, comprehensive, and easy-to-use
Java API for communicating with LDAP directory servers and performing
related tasks like reading and writing LDIF, encoding and decoding data
using base64 and ASN.1 BER, and performing secure communication. This
package contains the Standard Edition of the LDAP SDK, which is a
complete, general-purpose library for communicating with LDAPv3 directory
servers.
/*
* Copyright 2008-2018 Ping Identity Corporation
* All Rights Reserved.
*/
/*
* Copyright (C) 2008-2018 Ping Identity Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (GPLv2 only)
* or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see .
*/
package com.unboundid.util;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import static com.unboundid.util.UtilityMessages.*;
/**
* This class defines a file value pattern component, which may be used provide
* string values read from a specified local file. Values may be accessed in
* random or sequential order.
*/
final class FileValuePatternComponent
extends ValuePatternComponent
{
/**
* The serial version UID for this serializable class.
*/
private static final long serialVersionUID = 2773328295435703361L;
// A counter used to determine the index for the next value to return if
// accessing the file in sequential order.
private final AtomicLong sequentialCounter;
// Indicates whether to iterate through the file in sequential order.
private final boolean sequential;
// The lines that make up the data file.
private final String[] lines;
// The random number generator that will be used to seed the thread-local
// generators.
private final Random seedRandom;
// The random number generator that will be used by this component.
private final ThreadLocal random;
/**
* Creates a new file value pattern component with the provided information.
*
* @param path The path to the file from which to read the data.
* @param seed The value that will be used to seed the initial random
* number generator.
* @param sequential Indicates whether to iterate through the file in
* sequential order rather than at random.
*
* @throws IOException If a problem occurs while reading data from the
* specified file.
*/
FileValuePatternComponent(final String path, final long seed,
final boolean sequential)
throws IOException
{
// Create the random number generators that will be used.
this.sequential = sequential;
sequentialCounter = new AtomicLong(0L);
seedRandom = new Random(seed);
random = new ThreadLocal<>();
final ArrayList lineList = new ArrayList<>(100);
final BufferedReader reader = new BufferedReader(new FileReader(path));
try
{
while (true)
{
final String line = reader.readLine();
if (line == null)
{
break;
}
lineList.add(line);
}
}
finally
{
reader.close();
}
if (lineList.isEmpty())
{
throw new IOException(ERR_VALUE_PATTERN_COMPONENT_EMPTY_FILE.get());
}
lines = new String[lineList.size()];
lineList.toArray(lines);
}
/**
* {@inheritDoc}
*/
@Override()
void append(final StringBuilder buffer)
{
final int index;
if (sequential)
{
index = (int) (sequentialCounter.getAndIncrement() % lines.length);
}
else
{
Random r = random.get();
if (r == null)
{
r = new Random(seedRandom.nextLong());
random.set(r);
}
index = r.nextInt(lines.length);
}
buffer.append(lines[index]);
}
/**
* {@inheritDoc}
*/
@Override()
boolean supportsBackReference()
{
return true;
}
}