Example usage for java.util Arrays copyOf

List of usage examples for java.util Arrays copyOf

Introduction

In this page you can find the example usage for java.util Arrays copyOf.

Prototype

public static boolean[] copyOf(boolean[] original, int newLength) 

Source Link

Document

Copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length.

Usage

From source file:com.opengamma.analytics.math.surface.InterpolatedSurfaceAdditiveShiftFunction.java

/**
 * {@inheritDoc}/*from w  w w .  ja  va2  s .c om*/
 */
@Override
public InterpolatedDoublesSurface evaluate(final InterpolatedDoublesSurface surface, final double x,
        final double y, final double shift, final String newName) {
    Validate.notNull(surface, "surface");
    final double[] xData = surface.getXDataAsPrimitive();
    final double[] yData = surface.getYDataAsPrimitive();
    final double[] zData = surface.getZDataAsPrimitive();
    final int n = xData.length;
    for (int i = 0; i < n; i++) {
        if (Double.doubleToLongBits(xData[i]) == Double.doubleToLongBits(x)) {
            if (Double.doubleToLongBits(yData[i]) == Double.doubleToLongBits(y)) {
                final double[] shiftedZ = Arrays.copyOf(zData, n);
                shiftedZ[i] += shift;
                return InterpolatedDoublesSurface.from(xData, yData, shiftedZ, surface.getInterpolator(),
                        newName);
            }
        }
    }
    final double[] newX = new double[n + 1];
    final double[] newY = new double[n + 1];
    final double[] newZ = new double[n + 1];
    for (int i = 0; i < n; i++) {
        newX[i] = xData[i];
        newY[i] = yData[i];
        newZ[i] = zData[i];
    }
    newX[n] = x;
    newY[n] = y;
    newZ[n] = surface.getZValue(x, y) + shift;
    return InterpolatedDoublesSurface.from(newX, newY, newZ, surface.getInterpolator(), newName);
}

From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.TestPeriods.java

@Test
public void testRun03() throws InterruptedException {
    PrintStream printStream = new PrintStream(outputStream);
    this.periods = new Periods(50, printStream);

    new Thread(this.periods).start();

    Thread.sleep(400);//from  ww w . ja v a2 s .  c  o  m

    this.periods.stop();

    byte[] bytes = this.outputStream.toByteArray();
    assertTrue("The length is not correct.", bytes.length >= 6);
    assertArrayEquals("The arrays are not correct.", Arrays.copyOf(bytes, 6),
            new byte[] { '.', '.', '.', '.', '.', '.' });
}

From source file:com.laxser.blitz.web.impl.mapping.EngineGroupImpl.java

/**
 *  {@link Engine}  method  {@link ReqMethod#ALL}
 * //from w w w.j a  va  2  s .com
 * @param method
 * @param engine
 */
public void addEngine(ReqMethod method, LinkedEngine engine) {
    for (ReqMethod md : method.parse()) {
        LinkedEngine[] methodEngines = engines[md.ordinal()];
        if (methodEngines.length == 0) {
            methodEngines = new LinkedEngine[] { engine };
        } else {
            methodEngines = Arrays.copyOf(methodEngines, methodEngines.length + 1);
            methodEngines[methodEngines.length - 1] = engine;
        }
        engines[md.ordinal()] = methodEngines;
        engineCount++;
    }
    clearCache();
}

From source file:com.adobe.acs.commons.quickly.Command.java

public String[] getParams() {
    return Arrays.copyOf(this.params, this.params.length);
}

From source file:com.wrmsr.wava.yen.translation.UnitTranslation.java

public static Module translateModule(Name name, YModule module) {
    YMemory ymemory = module.getMemory();
    Memory memory = new Memory(ymemory.getInitial(),
            ymemory.getMax() >= 0 ? OptionalInt.of(ymemory.getMax()) : OptionalInt.empty(), ymemory
                    .getSegments().stream().map(
                            s -> new Segment(s.getOffset(),
                                    s.getData().length == s.getSize() ? s.getData()
                                            : Arrays.copyOf(s.getData(), s.getSize())))
                    .collect(toImmutableList()));
    Table table = new Table(module.getTable().getNames());
    Map<Name, Signature> functionSignatures = ImmutableMap.<Name, Signature>builder()
            .putAll(module.getFunctions().stream()
                    .map(f -> ImmutablePair.of(f.getName().get(), new Signature(f.getResult(), f.getParams())))
                    .collect(toImmutableList()))
            .putAll(module.getImports().stream()
                    .map(i -> ImmutablePair.of(i.getName().get(),
                            new Signature(i.getType().getResult(), i.getType().getParams())))
                    .collect(toImmutableList()))
            .build();//from   w  w  w.j  a  v a 2 s. c o  m
    Map<Name, Import> imports = module.getImports().stream()
            .map(i -> ImmutablePair.of(i.getName().get(),
                    new Import(i.getName().get(), i.getModule(), i.getBase(),
                            new Signature(i.getType().getResult(), i.getType().getParams()))))
            .collect(toImmutableMap());
    Map<Name, Export> exports = module.getExports().stream()
            .map(e -> ImmutablePair.of(e.getName().get(), new Export(e.getName().get(), e.getValue())))
            .collect(toImmutableMap());
    Map<Name, Function> functions = module.getFunctions().stream().parallel() // FIXME boo this man
            .map(f -> ImmutablePair.of(f.getName().get(), translateFunction(f, functionSignatures)))
            .sequential().collect(toImmutableMap());
    Set<Signature> declaredSignatures = module.getNamedFunctionTypes().stream()
            .map(i -> new Signature(i.getResult(), i.getParams())).collect(Collectors.toSet());
    return new Module(name, memory, table, declaredSignatures, imports, exports, functions);
}

From source file:edu.gsgp.utils.Utils.java

public static double getMedian(long[] array) {
    long[] auxArray = Arrays.copyOf(array, array.length);
    Arrays.sort(auxArray);/*from  w  ww . ja  va  2  s. c o  m*/
    // Even number
    if (auxArray.length % 2 == 0) {
        int secondElement = auxArray.length / 2;
        return (auxArray[secondElement - 1] + auxArray[secondElement]) / 2;
    } else {
        int element = (auxArray.length - 1) / 2;
        return auxArray[element];
    }
}

From source file:com.orange.clara.cloud.poc.s3.upload.UploadS3StreamImpl.java

@Override
public String upload(InputStream content, Blob blob) throws IOException {
    String bucketName = this.blobStoreContext.getSpringCloudBlobStore().getBucketName();
    String key = blob.getMetadata().getName();
    ContentMetadata metadata = blob.getMetadata().getContentMetadata();
    ObjectMetadataBuilder builder = ObjectMetadataBuilder.create().key(key)
            .contentType(MediaType.OCTET_STREAM.toString()).contentDisposition(key)
            .contentEncoding(metadata.getContentEncoding()).contentLanguage(metadata.getContentLanguage())
            .userMetadata(blob.getMetadata().getUserMetadata());
    String uploadId = this.s3Client.initiateMultipartUpload(bucketName, builder.build());
    Integer partNum = 1;/* www .j a  v a2s  . co  m*/
    Payload part = null;
    int bytesRead = 0;
    boolean shouldContinue = true;
    try {
        SortedMap<Integer, String> etags = Maps.newTreeMap();
        while (shouldContinue) {
            byte[] chunk = new byte[CHUNK_SIZE];
            bytesRead = ByteStreams.read(content, chunk, 0, chunk.length);
            if (bytesRead != chunk.length) {
                shouldContinue = false;
                chunk = Arrays.copyOf(chunk, bytesRead);
            }
            part = new ByteArrayPayload(chunk);
            prepareUploadPart(bucketName, key, uploadId, partNum, part, etags);
            partNum++;
        }
        return this.s3Client.completeMultipartUpload(bucketName, key, uploadId, etags);
    } catch (RuntimeException ex) {
        this.s3Client.abortMultipartUpload(bucketName, key, uploadId);
        throw ex;
    }
}

From source file:com.opengamma.analytics.math.surface.InterpolatedSurfaceMultiplicativeShiftFunction.java

/**
 * {@inheritDoc}//from w ww.ja v a2  s . c o  m
 */
@Override
public InterpolatedDoublesSurface evaluate(final InterpolatedDoublesSurface surface, final double x,
        final double y, final double percentage, final String newName) {
    Validate.notNull(surface, "surface");
    final double[] xData = surface.getXDataAsPrimitive();
    final double[] yData = surface.getYDataAsPrimitive();
    final double[] zData = surface.getZDataAsPrimitive();
    final int n = xData.length;
    for (int i = 0; i < n; i++) {
        if (Double.doubleToLongBits(xData[i]) == Double.doubleToLongBits(x)) {
            if (Double.doubleToLongBits(yData[i]) == Double.doubleToLongBits(y)) {
                final double[] shiftedZ = Arrays.copyOf(zData, n);
                shiftedZ[i] *= 1 + percentage;
                return InterpolatedDoublesSurface.from(xData, yData, shiftedZ, surface.getInterpolator(),
                        newName);
            }
        }
    }
    final double[] newX = new double[n + 1];
    final double[] newY = new double[n + 1];
    final double[] newZ = new double[n + 1];
    for (int i = 0; i < n; i++) {
        newX[i] = xData[i];
        newY[i] = yData[i];
        newZ[i] = zData[i];
    }
    newX[n] = x;
    newY[n] = y;
    newZ[n] = surface.getZValue(x, y) + percentage;
    return InterpolatedDoublesSurface.from(newX, newY, newZ, surface.getInterpolator(), newName);
}

From source file:com.opengamma.analytics.math.interpolation.ShapePreservingCubicSplineInterpolator.java

@Override
public PiecewisePolynomialResult interpolate(final double[] xValues, final double[] yValues) {

    ArgumentChecker.notNull(xValues, "xValues");
    ArgumentChecker.notNull(yValues, "yValues");

    ArgumentChecker.isTrue(xValues.length == yValues.length, "xValues length = yValues length");
    ArgumentChecker.isTrue(xValues.length > 2, "Data points should be more than 1");

    final int nDataPts = xValues.length;

    for (int i = 0; i < nDataPts; ++i) {
        ArgumentChecker.isFalse(Double.isNaN(xValues[i]), "xData containing NaN");
        ArgumentChecker.isFalse(Double.isInfinite(xValues[i]), "xData containing Infinity");
        ArgumentChecker.isFalse(Double.isNaN(yValues[i]), "yData containing NaN");
        ArgumentChecker.isFalse(Double.isInfinite(yValues[i]), "yData containing Infinity");
    }/*from w  w  w .j  ava2s . com*/

    for (int i = 0; i < nDataPts; ++i) {
        for (int j = i + 1; j < nDataPts; ++j) {
            ArgumentChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
        }
    }

    final double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts);
    final double[] yValuesSrt = Arrays.copyOf(yValues, nDataPts);
    ParallelArrayBinarySort.parallelBinarySort(xValuesSrt, yValuesSrt);

    final double[] intervals = intervalsCalculator(xValuesSrt);
    final double[] slopes = slopesCalculator(yValuesSrt, intervals);
    final double[] beta = betaCalculator(slopes);
    double[] first = firstDiffFinder(intervals, slopes);
    double[] rValues = rValuesCalculator(slopes, first);

    boolean correctSign = false;
    int it = 0;

    while (correctSign == false) {
        correctSign = signChecker(beta, rValues);
        if (correctSign == false) {
            first = firstDiffSweep(intervals, slopes, beta, first);
            rValues = rValuesCalculator(slopes, first);
        }
        ++it;
        if (it > 10) {
            throw new IllegalArgumentException("Spline is not found!");
        }
    }

    final double[] second = secondDiffFinder(intervals, beta, rValues);
    final double[] tau = tauFinder(intervals, slopes, beta, first, second);
    final double[] knots = knotsProvider(xValuesSrt, intervals, tau);

    final double[][] coefMatrix = solve(yValuesSrt, intervals, slopes, first, second, tau);

    for (int i = 0; i < coefMatrix.length; ++i) {
        double ref = 0.;
        final double interval = knots[i + 1] - knots[i];
        for (int j = 0; j < 4; ++j) {
            ref += coefMatrix[i][j] * Math.pow(interval, 3 - j);
            ArgumentChecker.isFalse(Double.isNaN(coefMatrix[i][j]), "Too large input");
            ArgumentChecker.isFalse(Double.isInfinite(coefMatrix[i][j]), "Too large input");
        }
        final double yVal = i == coefMatrix.length - 1 ? yValues[nDataPts - 1] : coefMatrix[i + 1][3];
        final double bound = Math.max(Math.abs(ref) + Math.abs(yVal), 1.e-1);
        ArgumentChecker.isTrue(Math.abs(ref - yVal) < ERROR * bound,
                "Input is too large/small or data points are too close");
    }

    return new PiecewisePolynomialResult(new DoubleMatrix1D(knots), new DoubleMatrix2D(coefMatrix), 4, 1);
}

From source file:org.carewebframework.common.StrUtil.java

/**
 * Splits a string using the specified delimiter.
 * //from  ww w  . j a  v  a 2 s  .c om
 * @param text The string to split.
 * @param delimiter The delimiter for the split operation.
 * @param count Specifies a minimum number of elements to be returned. If the result of the
 *            split operation results in fewer elements, the returned array is expanded to
 *            contain the minimum number of elements.
 * @param nonull If true, the returned array is guaranteed not to contain null values by
 *            replacing every occurrence of a null value with an empty string.
 * @return A string array containing the split values.
 */
public static String[] split(String text, String delimiter, int count, boolean nonull) {
    String[] pcs = text == null ? new String[count]
            : StringUtils.splitByWholeSeparatorPreserveAllTokens(text, delimiter);
    pcs = pcs.length >= count ? pcs : Arrays.copyOf(pcs, count);

    if (nonull) {
        for (int i = 0; i < pcs.length; i++) {
            if (pcs[i] == null) {
                pcs[i] = "";
            }
        }
    }

    return pcs;
}