Java Code Examples for org.apache.commons.lang.ArrayUtils.add()

The following are Jave code examples for showing how to use add() of the org.apache.commons.lang.ArrayUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: calendar-based-microsim   File: ArrayHandler.java   View Source Code Vote up 6 votes
/**
 * picks randomly non-repeating some elements from a source array and not in
 * another array. For example, pickRandomFromArray(new int[] {1,2,3,4,5,6},
 * new int[] {2,6}, 2) returns {1,5}.
 * 
 * @param srcArray
 *            the source array where elements will be picked from.
 * @param excludedArray
 *            the array containing elements that must not be picked.
 * @param nPick
 *            number of array to be picked.
 * @sRandom a random object generating controlled random numbers.
 * @return a subset of the source array that have no elements as specified
 *         in the excludedArray.
 */
public static int[] pickRandomFromArray(int[] srcArray, int[] excludedArray,
		int nPick, Random sRandom) {
	if (nPick <= 0 || srcArray == null || srcArray.length == 0) {
		return null;
       }
	int[] excluded = excludedArray;
	int[] finArr = new int[nPick];
	int[] remained = getNonCommonInArrays(srcArray,excluded);
	if (remained.length < nPick) {
		finArr = null;
       } else {
		for (int i = 0; i <= nPick - 1; i++) {
			if (remained.length == 1) {
				finArr[i] = remained[0];
               } else {
				//finArr[i] = remained[sRandom.nextInt(remained.length - 1)];
               	finArr[i] = remained[sRandom.nextInt(remained.length)];
               }
			excluded = ArrayUtils.add(excluded, finArr[i]);
			remained = getNonCommonInArrays(srcArray, excluded);
		}
	}
	return finArr;
}
 
Example 2
Project: calendar-based-microsim   File: ArrayHandler.java   View Source Code Vote up 6 votes
/**
 * returns values in an array (source array) that are not in another array
 * (comparing array). For example getNonCommonInArrays(new int[]
 * {1,2,3,6,8}, new String[] {3,7,2}) returns {1,6,8}.
 * 
 * @param array1
 *            source array.
 * @param array2
 *            comparing array.
 * @return values in source array that are not in comparing array.
 */
public static int[] getNonCommonInArrays(int[] array1, int[] array2) {
	int[] nonCommon = null;

	if (array2 == null) {
		return array1;
       }
	if (array1 == null) {
		return null;
       }

	for (int i1 = 0; i1 <= array1.length - 1; i1++) {
		boolean common = false;
		for (int i2 = 0; i2 <= array2.length - 1; i2++) {
			if (array1[i1] == array2[i2]) {
				common = true;
				break;
			}
           }
		if (!common) {
			nonCommon = ArrayUtils.add(nonCommon, array1[i1]);
           }
	}

	return nonCommon;
}
 
Example 3
Project: calendar-based-microsim   File: ArrayHandler.java   View Source Code Vote up 5 votes
/**
 * removes elements equal to a specified value from an original array.
 * 
 * @param tmparray
 *            the original integer array.
 * @param remVal
 *            the value to be removed.
 * @return subset of the original array (with out elements equal to the
 *         specified value)
 */
public static int[] removeValueInArray(int[] tmparray, int remVal) {
	int[] finarray = null;
	if (tmparray == null) {
		return finarray;
       }
	int[] idxVal = null;
	for (int i = 0; i < tmparray.length; i++) {
		if (tmparray[i] == remVal) {
			idxVal = ArrayUtils.add(idxVal, i);
           }
       }
	if (idxVal == null) {
		return tmparray;
       }
	int[] idxRemain = null;
	for (int i = 0; i < tmparray.length; i++) {
		if (ArrayUtils.indexOf(idxVal, i) == -1) {
			idxRemain = ArrayUtils.add(idxRemain, i);
           }
       }

	if (idxRemain == null) {
		return finarray;
       }

	finarray = extractArray(tmparray, idxRemain);

	return finarray;
}
 
Example 4
Project: fluxtion-examples   File: PriceOrderGenerator.java   View Source Code Vote up 5 votes
/**
 * Adds the cached currency to the array
 */
private void addCcy_() {
    CcyPair pair = configToAdd.pair;
    for (CcyConfig ccyConfig : configArray) {
        if (pair == ccyConfig.pair) {
            ccyConfig.bias = configToAdd.bias;
            ccyConfig.delayMircos = configToAdd.delayMircos;
            ccyConfig.orderRate = configToAdd.orderRate;
            ccyConfig.rejectRate = configToAdd.rejectRate;
            return;
        }
    }
    configArray = (CcyConfig[]) ArrayUtils.add(configArray, configToAdd);
}
 
Example 5
Project: SparkSeq   File: LDMerger.java   View Source Code Vote up 5 votes
protected VariantContext createMergedVariantContext( final VariantContext thisVC, final VariantContext nextVC, final byte[] ref, final GenomeLoc refLoc ) {
    final int thisStart = thisVC.getStart();
    final int nextStart = nextVC.getStart();
    byte[] refBases = new byte[]{};
    byte[] altBases = new byte[]{};
    refBases = ArrayUtils.addAll(refBases, thisVC.getReference().getBases());
    altBases = ArrayUtils.addAll(altBases, thisVC.getAlternateAllele(0).getBases());
    int locus;
    for( locus = thisStart + refBases.length; locus < nextStart; locus++ ) {
        final byte refByte = ref[locus - refLoc.getStart()];
        refBases = ArrayUtils.add(refBases, refByte);
        altBases = ArrayUtils.add(altBases, refByte);
    }
    refBases = ArrayUtils.addAll(refBases, ArrayUtils.subarray(nextVC.getReference().getBases(), locus > nextStart ? 1 : 0, nextVC.getReference().getBases().length)); // special case of deletion including the padding base of consecutive indel
    altBases = ArrayUtils.addAll(altBases, nextVC.getAlternateAllele(0).getBases());

    int iii = 0;
    if( refBases.length == altBases.length ) { // insertion + deletion of same length creates an MNP --> trim common prefix bases off the beginning of the allele
        while( iii < refBases.length && refBases[iii] == altBases[iii] ) { iii++; }
        if ( iii == refBases.length ) {
            // we've become a null allele, such as with CA/C + A/AA -> CA/CA => after trimming there's nothing left
            // so return a null variant context so we can eliminate the variants from consideration
            return null;
        }
    }


    final Allele refAllele = Allele.create( ArrayUtils.subarray(refBases, iii, refBases.length), true );
    final Allele altAllele =  Allele.create( ArrayUtils.subarray(altBases, iii, altBases.length), false );
    return new VariantContextBuilder("merged", thisVC.getChr(), thisVC.getStart() + iii, nextVC.getEnd(), Arrays.asList(refAllele, altAllele)).make();
}
 
Example 6
Project: ahar   File: ArchiveManager.java   View Source Code Vote up 5 votes
private String[] prepareArguments(Path harPath, Path[] fileList, String replication) {
    Object[] args = new String[]{"-archiveName", harPath.getName(), "-p", "/", "-r", replication};
    for (Path path : fileList) {
        args = ArrayUtils.add(args, makeRelativeToRoot(path.toString()));
    }
    args = ArrayUtils.add(args, new String(harPath.getParent().toString() + "/"));
    return (String[]) args;
}
 
Example 7
Project: alfresco-repository   File: ActivitiSpringTest.java   View Source Code Vote up 4 votes
private String[] getConfigLocations()
{
    String[] defaultLocations = ApplicationContextHelper.CONFIG_LOCATIONS;
    Object[] locations = ArrayUtils.add(defaultLocations, ACTIVITI_CONTEXT);
    return (String[]) locations;
}
 
Example 8
Project: kaltura-ce-sakai-extension   File: MediaServiceTest.java   View Source Code Vote up 4 votes
public void testUpdateCollectionMediaItems() {
    MediaCollection coll;
    List<MediaItem> items;
    String[] kalturaIds;

    // SUPER ADMIN
    external.currentUserId = KalturaAPIServiceStub.ADMIN_USER_ID;

    coll = service.getCollection(kalturaAPIService.mcAd.getIdStr(), 0, -1);
    assertNotNull(coll);
    assertTrue(coll.isAddItems());
    items = coll.getItems();
    assertEquals(5, items.size());
    kalturaIds = coll.getKalturaIds().toArray(new String[items.size()]);
    service.updateCollectionMediaItems(coll.getId(), kalturaIds);
    coll = service.getCollection(kalturaAPIService.mcAd.getIdStr(), 0, -1);
    assertEquals(5, coll.getItems().size()); // no change in size

    // test adding one
    coll = service.getCollection(kalturaAPIService.mcAd.getIdStr(), 0, -1);
    assertNotNull(coll);
    assertTrue(coll.isAddItems());
    items = coll.getItems();
    assertEquals(5, items.size());
    kalturaIds = coll.getKalturaIds().toArray(new String[items.size()]);
    kalturaIds = (String[]) ArrayUtils.add(kalturaIds, "kid7");
    service.updateCollectionMediaItems(coll.getId(), kalturaIds);
    coll = service.getCollection(kalturaAPIService.mcAd.getIdStr(), 0, -1);
    assertEquals(6, coll.getItems().size()); // +1 size
    assertTrue(coll.getKalturaIds().contains("kid7"));

    // test removing one
    coll = service.getCollection(kalturaAPIService.mcAd.getIdStr(), 0, -1);
    assertNotNull(coll);
    assertTrue(coll.isAddItems());
    items = coll.getItems();
    assertEquals(6, items.size());
    kalturaIds = coll.getKalturaIds().toArray(new String[items.size()]);
    kalturaIds = (String[]) ArrayUtils.removeElement(kalturaIds, "kid7");
    service.updateCollectionMediaItems(coll.getId(), kalturaIds);
    coll = service.getCollection(kalturaAPIService.mcAd.getIdStr(), 0, -1);
    assertEquals(5, coll.getItems().size()); // -1 size
    assertFalse(coll.getKalturaIds().contains("kid7"));

    // testing adding and removing
    coll = service.getCollection(kalturaAPIService.mcAd.getIdStr(), 0, -1);
    assertNotNull(coll);
    assertTrue(coll.isAddItems());
    items = coll.getItems();
    assertEquals(5, items.size());
    kalturaIds = coll.getKalturaIds().toArray(new String[items.size()]);
    kalturaIds = (String[]) ArrayUtils.removeElement(kalturaIds, "kid5");
    kalturaIds = (String[]) ArrayUtils.add(kalturaIds, "kidA");
    kalturaIds = (String[]) ArrayUtils.add(kalturaIds, "kidB");
    service.updateCollectionMediaItems(coll.getId(), kalturaIds);
    coll = service.getCollection(kalturaAPIService.mcAd.getIdStr(), 0, -1);
    assertEquals(6, coll.getItems().size()); // -1 +2 size
    assertFalse(coll.getKalturaIds().contains("kid5"));
    assertTrue(coll.getKalturaIds().contains("kidA"));
    assertTrue(coll.getKalturaIds().contains("kidB"));
}