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.interpolation.MonotoneConvexSplineInterpolator.java

/**
 * Determine r(t)t = \int _{xValues_0}^{x} f(s) ds  for t >= min{xValues}
 * Extrapolation by a linear function in the region t > max{xValues}. To employ this extrapolation, use interpolate methods in this class. 
 * @param xValues Data t_i/* w  w w  .jav a2  s  .  co  m*/
 * @param yValues Data r_i*t_i
 * @return PiecewisePolynomialResult for r(t)t
 */
@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 > 1, "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");
    }

    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");
        }
    }

    for (int i = 0; i < nDataPts; ++i) {
        if (xValues[i] == 0.) {
            ArgumentChecker.isTrue(yValues[i] == 0., "r_i * t_i = 0 if t_i =0");
        }
    }

    double[] spotTmp = new double[nDataPts];
    for (int i = 0; i < nDataPts; ++i) {
        spotTmp[i] = xValues[i] == 0. ? 0. : yValues[i] / xValues[i];
    }

    _time = Arrays.copyOf(xValues, nDataPts);
    _spotRates = Arrays.copyOf(spotTmp, nDataPts);
    ParallelArrayBinarySort.parallelBinarySort(_time, _spotRates);

    final DoubleMatrix2D coefMatrix = solve(_time, _spotRates);
    final DoubleMatrix2D coefMatrixIntegrate = integration(_time, coefMatrix.getData());

    for (int i = 0; i < coefMatrixIntegrate.getNumberOfRows(); ++i) {
        for (int j = 0; j < coefMatrixIntegrate.getNumberOfColumns(); ++j) {
            ArgumentChecker.isFalse(Double.isNaN(coefMatrixIntegrate.getData()[i][j]), "Too large input");
            ArgumentChecker.isFalse(Double.isInfinite(coefMatrixIntegrate.getData()[i][j]), "Too large input");
        }
    }

    return new PiecewisePolynomialResult(new DoubleMatrix1D(_time), coefMatrixIntegrate,
            coefMatrixIntegrate.getNumberOfColumns(), 1);
}

From source file:com.github.rvesse.airline.help.sections.factories.CommonSectionsFactory.java

@Override
public HelpSection createSection(Annotation annotation) {
    if (annotation instanceof Examples) {
        // Examples
        Examples ex = (Examples) annotation;
        return new ExamplesSection(ex.examples(), ex.descriptions());
    } else if (annotation instanceof Discussion) {
        // Discussion
        return new DiscussionSection(((Discussion) annotation).paragraphs());
    } else if (annotation instanceof ExitCodes) {
        // Exit Codes
        ExitCodes exits = (ExitCodes) annotation;
        return new ExitCodesSection(exits.codes(), exits.descriptions());
    } else if (annotation instanceof HideSection) {
        // Hide Section
        // Used to hide inherited section
        HideSection hide = (HideSection) annotation;
        return new BasicSection(hide.title(), 0, null, null, HelpFormat.NONE_PRINTABLE, new String[0]);
    } else if (annotation instanceof ProseSection) {
        // Prose Section
        ProseSection prose = (ProseSection) annotation;
        return new com.github.rvesse.airline.help.sections.common.ProseSection(prose.title(),
                prose.suggestedOrder(), prose.paragraphs());
    } else if (annotation instanceof Copyright) {
        // Copyright Section
        Copyright copyright = (Copyright) annotation;
        String line = String.format("Copyright (c) %s %s%s", copyright.holder(), copyright.startYear(),
                copyright.endYear() > copyright.startYear() ? String.format("-%s", copyright.endYear()) : "");
        return new com.github.rvesse.airline.help.sections.common.ProseSection(CommonSections.TITLE_COPYRIGHT,
                CommonSections.ORDER_COPYRIGHT, new String[] { line });
    } else if (annotation instanceof License) {
        // License section
        License license = (License) annotation;
        String[] data = Arrays.copyOf(license.paragraphs(),
                StringUtils.isNotBlank(license.url()) ? license.paragraphs().length + 1
                        : license.paragraphs().length);
        if (StringUtils.isNotBlank(license.url())) {
            data[data.length - 1] = String.format("Please see %s for more information", license.url());
        }/*w  ww.  j a  va  2s  .c  o  m*/
        return new com.github.rvesse.airline.help.sections.common.ProseSection(CommonSections.TITLE_LICENSE,
                CommonSections.ORDER_LICENSE, data);
    } else if (annotation instanceof Version) {
        // Version section
        Version version = (Version) annotation;
        return new VersionSection(version.sources(), version.componentProperty(), version.versionProperty(),
                version.buildProperty(), version.dateProperty(), version.additionalProperties(),
                version.additionalTitles(), version.suppressOnError(), version.tabular());
    }
    return null;
}

From source file:com.sonicle.webtop.core.util.IdentifierUtils.java

/**
 * @deprecated use com.sonicle.commons.IdentifierUtils.generateSecretKey instead
 * @return//from w w  w. j a  v  a2s  .  co  m
 */
@Deprecated
public static synchronized String generateSecretKey() {
    try {
        byte[] buffer = new byte[80 / 8];
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        sr.nextBytes(buffer);
        byte[] secretKey = Arrays.copyOf(buffer, 80 / 8);
        byte[] encodedKey = new Base32().encode(secretKey);
        return new String(encodedKey).toLowerCase();
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }
}

From source file:com.github.gdfm.shobaidogu.StatsUtils.java

/**
 * Compute Discount Cumulative Gain for a relevance vector.
 * /*from   www  .ja  v a  2s . c o  m*/
 * @param relevance
 *          the vector or relevance values.
 * @return DCG.
 */
public static double[] computeDCG(double[] relevance) {
    checkNotNull(relevance);
    checkArgument(relevance.length > 0);
    double[] dcg = Arrays.copyOf(relevance, relevance.length);
    for (int i = 1; i < dcg.length; i++)
        dcg[i] = dcg[i - 1] + dcg[i] / DoubleMath.log2(i + 1);
    return dcg;
}

From source file:hivemall.io.DenseModel.java

private void ensureCapacity(final int index) {
    if (index >= size) {
        int bits = MathUtils.bitsRequired(index);
        int newSize = (1 << bits) + 1;
        int oldSize = size;
        logger.info("Expands internal array size from " + oldSize + " to " + newSize + " (" + bits + " bits)");
        this.size = newSize;
        this.weights = Arrays.copyOf(weights, newSize);
        if (covars != null) {
            this.covars = Arrays.copyOf(covars, newSize);
            Arrays.fill(covars, oldSize, newSize, 1.f);
        }/*w w  w .j a va  2s .  c  o  m*/
        if (sum_of_squared_gradients != null) {
            this.sum_of_squared_gradients = Arrays.copyOf(sum_of_squared_gradients, newSize);
        }
        if (sum_of_squared_delta_x != null) {
            this.sum_of_squared_delta_x = Arrays.copyOf(sum_of_squared_delta_x, newSize);
        }
        if (sum_of_gradients != null) {
            this.sum_of_gradients = Arrays.copyOf(sum_of_gradients, newSize);
        }
        if (clocks != null) {
            this.clocks = Arrays.copyOf(clocks, newSize);
            this.deltaUpdates = Arrays.copyOf(deltaUpdates, newSize);
        }
    }
}

From source file:libepg.common.packet.TestPacket_BitError.java

public List<byte[]> getList_ContainError_WrongSync() {
    List<byte[]> list_ContainError_WrongSync = new ArrayList<>();
    //??/* w  ww  .java 2s.c o m*/
    byte[] p_20_ws = Arrays.copyOf(p_20, p_20.length);
    p_20_ws[0] = 0x44;
    byte[] p_21_ws = Arrays.copyOf(p_21, p_21.length);
    p_21_ws[0] = 0x45;
    list_ContainError_WrongSync.add(p_0);
    list_ContainError_WrongSync.add(p_1);
    list_ContainError_WrongSync.add(p_2);
    list_ContainError_WrongSync.add(p_3);
    list_ContainError_WrongSync.add(p_4);
    list_ContainError_WrongSync.add(p_5);
    list_ContainError_WrongSync.add(p_6);
    list_ContainError_WrongSync.add(p_7);
    list_ContainError_WrongSync.add(p_8);
    list_ContainError_WrongSync.add(p_9);
    list_ContainError_WrongSync.add(p_10);
    list_ContainError_WrongSync.add(p_11);
    list_ContainError_WrongSync.add(p_12);
    list_ContainError_WrongSync.add(p_13);
    list_ContainError_WrongSync.add(p_14);
    list_ContainError_WrongSync.add(p_15);
    list_ContainError_WrongSync.add(p_16);
    list_ContainError_WrongSync.add(p_17);
    list_ContainError_WrongSync.add(p_18);
    list_ContainError_WrongSync.add(p_19);
    list_ContainError_WrongSync.add(p_20_ws);
    list_ContainError_WrongSync.add(p_21_ws);
    list_ContainError_WrongSync.add(p_22);
    return list_ContainError_WrongSync;
}

From source file:com.l2jfree.gameserver.datatables.SkillTable.java

private SkillTable() {
    final List<L2Skill> skills = DocumentEngine.loadSkills();
    _log.info("SkillTable: Loaded " + skills.size() + " skill templates from XML files.");

    int highestId = 0;
    for (L2Skill skill : skills)
        if (highestId < skill.getId())
            highestId = skill.getId();// w  w  w  .  ja va  2s .c om

    _maxLevels = new int[highestId + 1];

    int[] highestLevels = new int[highestId + 1];
    for (L2Skill skill : skills) {
        if (highestLevels[skill.getId()] < skill.getLevel())
            highestLevels[skill.getId()] = skill.getLevel();

        if (_maxLevels[skill.getId()] < skill.getLevel() && skill.getLevel() < 100)
            _maxLevels[skill.getId()] = skill.getLevel();
    }

    // clear previously stored skills
    for (SkillInfo[] infos : SKILL_INFOS)
        if (infos != null)
            for (SkillInfo info : infos)
                if (info != null)
                    info._skill = null;

    _skillTable = new L2Skill[highestId + 1][];

    SKILL_INFOS = Arrays.copyOf(SKILL_INFOS, Math.max(SKILL_INFOS.length, highestId + 1));

    for (int i = 0; i < highestLevels.length; i++) {
        final int highestLevel = highestLevels[i];

        if (highestLevel < 1)
            continue;

        _skillTable[i] = new L2Skill[highestLevel + 1];

        if (SKILL_INFOS[i] == null)
            SKILL_INFOS[i] = new SkillInfo[highestLevel + 1];
        else
            SKILL_INFOS[i] = Arrays.copyOf(SKILL_INFOS[i], Math.max(SKILL_INFOS[i].length, highestLevel + 1));
    }

    for (L2Skill skill : skills) {
        _skillTable[skill.getId()][skill.getLevel()] = skill;

        if (SKILL_INFOS[skill.getId()][skill.getLevel()] == null)
            SKILL_INFOS[skill.getId()][skill.getLevel()] = new SkillInfo(skill.getId(), skill.getLevel());

        SKILL_INFOS[skill.getId()][skill.getLevel()]._skill = skill;
    }

    int length = _skillTable.length;
    for (L2Skill[] array : _skillTable)
        if (array != null)
            length += array.length;

    _log.info("SkillTable: Occupying arrays for " + length + ".");

    SingletonHolder.INSTANCE = this;

    Map<Integer, L2Skill> skillsByUID = new HashMap<Integer, L2Skill>();

    for (L2Skill skill : skills) {
        try {
            L2Skill old = skillsByUID.put(SkillTable.getSkillUID(skill), skill);

            if (old != null)
                _log.warn("Overlapping UIDs for: " + old + ", " + skill, new IllegalStateException());

            skill.validate();
        } catch (Exception e) {
            _log.warn(skill, e);
        }
    }

    for (L2Skill skill0 : skills) {
        if (!(skill0 instanceof L2SkillLearnSkill))
            continue;

        L2SkillLearnSkill skill = (L2SkillLearnSkill) skill0;

        for (int i = 0; i < skill.getNewSkillId().length; i++) {
            final L2Skill learnedSkill = getInfo(skill.getNewSkillId()[i], skill.getNewSkillLvl()[i]);

            if (learnedSkill != null)
                _learnedSkills.add(learnedSkill);
        }
    }

    // checking for skill enchantment mismatch

    // in XMLs
    final TreeSet<String> skillEnchantsInXMLs = new TreeSet<String>();

    // reusing
    final Map<Integer, Set<Integer>> enchantLevelsByEnchantType = new HashMap<Integer, Set<Integer>>();

    for (int skillId = 0; skillId < _skillTable.length; skillId++) {
        final L2Skill[] skillsById = _skillTable[skillId];

        if (skillsById == null)
            continue;

        for (final L2Skill skill : skillsById) {
            if (skill == null || skill.getLevel() < 100)
                continue;

            final int enchantType = skill.getLevel() / 100;
            final int enchantLevel = skill.getLevel() % 100;

            Set<Integer> enchantLevels = enchantLevelsByEnchantType.get(enchantType);

            if (enchantLevels == null)
                enchantLevelsByEnchantType.put(enchantType, enchantLevels = new FastSet<Integer>(30));

            enchantLevels.add(enchantLevel);
        }

        for (Map.Entry<Integer, Set<Integer>> entry : enchantLevelsByEnchantType.entrySet()) {
            final int enchantType = entry.getKey();
            final Set<Integer> enchantLevels = entry.getValue();

            if (enchantLevels.isEmpty())
                continue;

            final String s = "Skill ID: " + skillId + " - EnchantType: enchant" + enchantType + " - Levels: "
                    + enchantLevels.size();

            boolean valid = true;

            for (int skillLvl = 1; skillLvl <= 30; skillLvl++) {
                if (!enchantLevels.remove(skillLvl)) {
                    if (skillLvl == 16 && enchantLevels.isEmpty())
                        break;
                    _log.warn("Missing skill enchant level in XMLs for " + s + " - Level: " + skillLvl);
                    valid = false;
                }
            }

            if (!enchantLevels.isEmpty())
                _log.warn("Extra skill enchant levels in XMLs for " + s + " - Levels: " + enchantLevels);
            else if (valid)
                skillEnchantsInXMLs.add(s);

            // reusing
            enchantLevels.clear();
        }
    }

    // in database
    final TreeSet<String> skillEnchantsInDatabase = new TreeSet<String>();

    for (L2EnchantSkillLearn skillLearn : SkillTreeTable.getInstance().getSkillEnchantments()) {
        final int skillId = skillLearn.getId();
        final List<EnchantSkillDetail>[] details = skillLearn.getEnchantRoutes();

        if (details.length == 0)
            _log.warn("Invalid skill enchant data in database for Skill ID: " + skillId);

        for (int indexingEnchantType = 0; indexingEnchantType < details.length; indexingEnchantType++) {
            final List<EnchantSkillDetail> route = details[indexingEnchantType];

            if (route == null)
                continue;

            final String s = "Skill ID: " + skillId + " - EnchantType: enchant" + (indexingEnchantType + 1)
                    + " - Levels: " + route.size();

            if (route.size() != 30 && route.size() != 15)
                _log.warn("Invalid skill enchant data in database for " + s);
            else
                skillEnchantsInDatabase.add(s);
        }
    }

    // comparing the results
    for (String skillEnchant : skillEnchantsInXMLs)
        if (!skillEnchantsInDatabase.remove(skillEnchant))
            _log.warn("Missing skill enchant data in database for " + skillEnchant);

    for (String skillEnchant : skillEnchantsInDatabase)
        _log.warn("Missing skill enchant data in XMLs for " + skillEnchant);

    // just validation
    for (L2EnchantSkillLearn skillLearn : SkillTreeTable.getInstance().getSkillEnchantments()) {
        final int skillId = skillLearn.getId();
        final List<EnchantSkillDetail>[] details = skillLearn.getEnchantRoutes();
        final int maxLevel = getMaxLevel(skillId);

        if (skillLearn.getBaseLevel() != maxLevel)
            _log.warn("Invalid `base_lvl` skill enchant data in database for Skill ID: " + skillId);

        for (int indexingEnchantType = 0; indexingEnchantType < details.length; indexingEnchantType++) {
            final List<EnchantSkillDetail> route = details[indexingEnchantType];

            if (route == null)
                continue;

            final String s = "Skill ID: " + skillId + " - EnchantType: enchant" + (indexingEnchantType + 1)
                    + " - Levels: " + route.size();

            int index = 1;
            int expectedMinSkillLevel = maxLevel;

            for (EnchantSkillDetail detail : route) {
                if (detail.getLevel() % 100 != index)
                    _log.warn("Invalid `level` skill enchant data in database for " + s);

                if (detail.getMinSkillLevel() != expectedMinSkillLevel)
                    _log.warn("Invalid `min_skill_lvl` skill enchant data in database for " + s);

                index++;
                expectedMinSkillLevel = detail.getLevel();
            }
        }
    }
}

From source file:edu.stevens.cpe.reservoir.translate.HSA.java

public double[] encode2() {
    //Make copy because we are going to modify it
    double[] _analog = Arrays.copyOf(analog, analog.length);

    double[] output = new double[_analog.length];

    for (int i = 0; i < _analog.length; i++) {

        //Compute errors
        double error1 = 0;
        double error2 = 0;
        for (int j = 0; j < filter.length; j++) {
            if (i + j < _analog.length) {
                error1 += Math.abs(_analog[i + j] - filter[j]);
                error2 += Math.abs(_analog[i + j]);
            }/*from w w  w  .  j a va  2  s  .  co  m*/
        }

        if (error1 <= (error2 - THRESHOLD)) {
            output[i] = 1;
            for (int j = 0; j < filter.length; j++) {
                if (i + j < _analog.length) {
                    _analog[i + j] -= filter[j];
                }
            }
        } else {
            output[i] = 0;
        }

    }

    return output;
}

From source file:ddf.catalog.metacard.duplication.DuplicationValidator.java

/**
 * Setter for the list of attributes to test for duplication in the local catalog.  Resulting
 * attributes will cause the {@link ddf.catalog.data.impl.BasicTypes#VALIDATION_ERRORS} attribute
 * to be set on the metacard./* ww  w  . jav a 2s . c  o  m*/
 *
 * @param attributeStrings
 */
public void setErrorOnDuplicateAttributes(String[] attributeStrings) {
    if (attributeStrings != null) {
        this.errorOnDuplicateAttributes = Arrays.copyOf(attributeStrings, attributeStrings.length);
    }
}

From source file:fitnesse.slim.test.TestSlim.java

public String[] getStringArray() {
    return Arrays.copyOf(stringArray, stringArray.length);
}