Java Code Examples for java.util.Collections#replaceAll()

The following examples show how to use java.util.Collections#replaceAll() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Joins the elements of the provided array into a single String containing
 * the provided list of elements.
 *
 * @param <T> type.
 * @param list the list of objects to join.
 * @param separator the separator string.
 * @param nullReplacement the value to replace nulls in list with.
 * @return the joined string.
 */
public static <T> String join( List<T> list, String separator, T nullReplacement )
{
    if ( list == null )
    {
        return null;
    }

    List<T> objects = new ArrayList<>( list );

    if ( nullReplacement != null )
    {
        Collections.replaceAll( objects, null, nullReplacement );
    }

    return StringUtils.join( objects, separator );
}
 
Example 2
Source Project: cloudstack   File: FirewallManagerImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@ActionEvent(eventType = EventTypes.EVENT_FIREWALL_EGRESS_OPEN, eventDescription = "creating egress firewall rule for network", create = true)
public FirewallRule createEgressFirewallRule(FirewallRule rule) throws NetworkRuleConflictException {
    Account caller = CallContext.current().getCallingAccount();

    Network network = _networkDao.findById(rule.getNetworkId());
    if (network.getGuestType() == Network.GuestType.Shared) {
        throw new InvalidParameterValueException("Egress firewall rules are not supported for " + network.getGuestType() + "  networks");
    }

    List<String> sourceCidrs = rule.getSourceCidrList();
    if (sourceCidrs != null && !sourceCidrs.isEmpty())
    Collections.replaceAll(sourceCidrs, "0.0.0.0/0", network.getCidr());

    return createFirewallRule(null, caller, rule.getXid(), rule.getSourcePortStart(), rule.getSourcePortEnd(), rule.getProtocol(), sourceCidrs, rule.getDestinationCidrList(),
            rule.getIcmpCode(), rule.getIcmpType(), null, rule.getType(), rule.getNetworkId(), rule.getTrafficType(), rule.isDisplay());
}
 
Example 3
Source Project: Bats   File: LogicVisitor.java    License: Apache License 2.0 5 votes vote down vote up
public static void collect(RexNode node, RexNode seek, Logic logic,
    List<Logic> logicList) {
  node.accept(new LogicVisitor(seek, logicList), logic);
  // Convert FALSE (which can only exist within LogicVisitor) to
  // UNKNOWN_AS_TRUE.
  Collections.replaceAll(logicList, Logic.FALSE, Logic.UNKNOWN_AS_TRUE);
}
 
Example 4
Source Project: Quicksql   File: LogicVisitor.java    License: MIT License 5 votes vote down vote up
public static void collect(RexNode node, RexNode seek, Logic logic,
    List<Logic> logicList) {
  node.accept(new LogicVisitor(seek, logicList), logic);
  // Convert FALSE (which can only exist within LogicVisitor) to
  // UNKNOWN_AS_TRUE.
  Collections.replaceAll(logicList, Logic.FALSE, Logic.UNKNOWN_AS_TRUE);
}
 
Example 5
private String convertOutputToJsonString(List<String> listUJsonOutput) {
    // go list -u -json does not provide data in a format that can be consumed by gson
    Collections.replaceAll(listUJsonOutput, "}", "},");
    String goModGraphAsString = String.join(System.lineSeparator(), listUJsonOutput);
    int lastCloseBrace = goModGraphAsString.lastIndexOf("},");
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("[" + System.lineSeparator());
    stringBuilder.append(goModGraphAsString.substring(0, lastCloseBrace));
    stringBuilder.append("}");
    stringBuilder.append(goModGraphAsString.substring(lastCloseBrace + 2));
    stringBuilder.append(System.lineSeparator() + "]");

    return stringBuilder.toString();
}
 
Example 6
public static void main(String[] args) {

    //create a Vector object
    Vector v = new Vector();

    //Add elements to Vector
    v.add("A");
    v.add("B");
    v.add("A");
    v.add("C");
    v.add("D");

    System.out.println("Vector Contains : " + v);

    /*
      To replace all occurrences of specified element of Java Vector use,
      static boolean replaceAll(List list, Object oldVal, Object newVal) method
      of Collections class.

      This method returns true if the list contained one more elements replaced.

    */

    Collections.replaceAll(v, "A", "Replace All");

    System.out.println("After Replace All, Vector Contains : " + v);
  }
 
Example 7
public static void main(String[] args) {

    //create an ArrayList object
    ArrayList arrayList = new ArrayList();

    //Add elements to Arraylist
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("A");
    arrayList.add("C");
    arrayList.add("D");

    System.out.println("ArrayList Contains : " + arrayList);

    /*
      To replace all occurrences of specified element of Java ArrayList use,
      static boolean replaceAll(List list, Object oldVal, Object newVal) method
      of Collections class.

      This method returns true if the list contained one more elements replaced.

    */

    Collections.replaceAll(arrayList, "A", "Replace All");

    System.out.println("After Replace All, ArrayList Contains : " + arrayList);
  }
 
Example 8
Source Project: sailfish-core   File: TestScriptsBean.java    License: Apache License 2.0 5 votes vote down vote up
public void onChange(MatrixAdapter newMatrixAdapter) {
	// replace matrixAdapter by updated one. (Search by matrixAdapter.id - refer to MatrixAdapter.equals() method)
    Collections.replaceAll(matrixAdapterList, newMatrixAdapter, newMatrixAdapter);
	try {
		scriptRunsBean.onChangeMatrixAdapter(newMatrixAdapter.clone());
	} catch (CloneNotSupportedException e) {
		logger.error(e.getMessage(), e);
	}
}
 
Example 9
public static void main(String[] args) {
    List<String> all = new ArrayList<String>();    // 返回空的 List集合
    Collections.addAll(all, "MLDN", "LXH", "mldnjava");
    if (Collections.replaceAll(all, "LXH", "李兴华")) {// 替换内容
        System.out.println("内容替换成功!");
    }
    System.out.print("替换之后的结果:");
    System.out.print(all);
}
 
Example 10
public static void main(String[] args) {

        //create a Vector object
        Vector v = new Vector();

        //Add elements to Vector
        v.add("A");
        v.add("B");
        v.add("A");
        v.add("C");
        v.add("D");

        System.out.println("Vector Contains : " + v);

    /*
      To replace all occurrences of specified element of Java Vector use,
      static boolean replaceAll(List list, Object oldVal, Object newVal) method
      of Collections class.

      This method returns true if the list contained one more elements replaced.

    */

        Collections.replaceAll(v, "A", "Replace All");

        System.out.println("After Replace All, Vector Contains : " + v);
    }
 
Example 11
public static void main(String[] args) {

        //create an ArrayList object
        ArrayList arrayList = new ArrayList();

        //Add elements to Arraylist
        arrayList.add("A");
        arrayList.add("B");
        arrayList.add("A");
        arrayList.add("C");
        arrayList.add("D");

        System.out.println("ArrayList Contains : " + arrayList);

    /*
      To replace all occurrences of specified element of Java ArrayList use,
      static boolean replaceAll(List list, Object oldVal, Object newVal) method
      of Collections class.

      This method returns true if the list contained one more elements replaced.

    */

        Collections.replaceAll(arrayList, "A", "Replace All");

        System.out.println("After Replace All, ArrayList Contains : " + arrayList);
    }
 
Example 12
Source Project: lucene-solr   File: CursorPagingTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Given a list of fieldNames, builds up a random sort string which is guaranteed to
 * have at least 3 clauses, ending with the "id" field for tie breaking
 */
public static String buildRandomSort(final Collection<String> fieldNames) {

  ArrayList<String> shuffledNames = new ArrayList<>(fieldNames);
  Collections.replaceAll(shuffledNames, "id", "score");
  Collections.shuffle(shuffledNames, random());

  final StringBuilder result = new StringBuilder();
  final int numClauses = TestUtil.nextInt(random(), 2, 5);

  for (int i = 0; i < numClauses; i++) {
    String field = shuffledNames.get(i);

    // wrap in a function sometimes
    if ( ! "score".equals(field) && 0 == TestUtil.nextInt(random(), 0, 7)) {
      // specific function doesn't matter, just proving that we can handle the concept.
      // but we do have to be careful with non numeric fields
      if (field.contains("float") || field.contains("double")
          || field.contains("int") || field.contains("long")) {
        field = "abs(" + field + ")";
      } else {
        field = "if(exists(" + field + "),47,83)";
      }
    }
    result.append(field).append(random().nextBoolean() ? " asc, " : " desc, ");
  }
  result.append("id").append(random().nextBoolean() ? " asc" : " desc");
  return result.toString();
}
 
Example 13
public BulkResultSet getResultSet(InputStream input) throws IOException {
    CsvReader reader = new CsvReader(new InputStreamReader(input), getDelimitedChar(columnDelimiter));
    List<String> baseFileHeader = null;
    if (reader.readRecord()) {
        baseFileHeader = Arrays.asList(reader.getValues());
        Collections.replaceAll(baseFileHeader, "sf__Id", "salesforce_id");
        Collections.replaceAll(baseFileHeader, "sf__Created", "salesforce_created");
    }
    return new BulkResultSet(reader, baseFileHeader);
}
 
Example 14
Source Project: calcite   File: LogicVisitor.java    License: Apache License 2.0 5 votes vote down vote up
public static void collect(RexNode node, RexNode seek, Logic logic,
    List<Logic> logicList) {
  node.accept(new LogicVisitor(seek, logicList), logic);
  // Convert FALSE (which can only exist within LogicVisitor) to
  // UNKNOWN_AS_TRUE.
  Collections.replaceAll(logicList, Logic.FALSE, Logic.UNKNOWN_AS_TRUE);
}
 
Example 15
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/tables/{tableName}/replaceInstance")
@ApiOperation(value = "Replace an instance in the instance partitions")
public Map<InstancePartitionsType, InstancePartitions> replaceInstance(
    @ApiParam(value = "Name of the table") @PathParam("tableName") String tableName,
    @ApiParam(value = "OFFLINE|CONSUMING|COMPLETED") @QueryParam("type") @Nullable InstancePartitionsType instancePartitionsType,
    @ApiParam(value = "Old instance to be replaced", required = true) @QueryParam("oldInstanceId") String oldInstanceId,
    @ApiParam(value = "New instance to replace with", required = true) @QueryParam("newInstanceId") String newInstanceId) {
  Map<InstancePartitionsType, InstancePartitions> instancePartitionsMap =
      getInstancePartitions(tableName, instancePartitionsType);
  Iterator<InstancePartitions> iterator = instancePartitionsMap.values().iterator();
  while (iterator.hasNext()) {
    InstancePartitions instancePartitions = iterator.next();
    boolean oldInstanceFound = false;
    Map<String, List<String>> partitionToInstancesMap = instancePartitions.getPartitionToInstancesMap();
    for (List<String> instances : partitionToInstancesMap.values()) {
      oldInstanceFound |= Collections.replaceAll(instances, oldInstanceId, newInstanceId);
    }
    if (oldInstanceFound) {
      persistInstancePartitionsHelper(instancePartitions);
    } else {
      iterator.remove();
    }
  }
  if (instancePartitionsMap.isEmpty()) {
    throw new ControllerApplicationException(LOGGER, "Failed to find the old instance", Response.Status.NOT_FOUND);
  } else {
    return instancePartitionsMap;
  }
}
 
Example 16
Source Project: Onosendai   File: Column.java    License: Apache License 2.0 5 votes vote down vote up
public Column replaceAccount (final Account newAccount, final InternalColumnType ict) {
	final ColumnFeed oldFeed = ict.findInFeeds(getFeeds());
	if (oldFeed == null) throw new IllegalArgumentException("ICT " + ict + " not found in feeds: " + getFeeds());
	final List<ColumnFeed> newFeeds = new ArrayList<ColumnFeed>(getFeeds());
	Collections.replaceAll(newFeeds, oldFeed, new ColumnFeed(newAccount.getId(), oldFeed.getResource()));
	return new Column(getId(), getTitle(), new LinkedHashSet<ColumnFeed>(newFeeds), getRefreshIntervalMins(),
			getExcludeColumnIds(), isHideRetweets(), getNotificationStyle(), getInlineMediaStyle(), isHdMedia());
}
 
Example 17
Source Project: sailfish-core   File: ScriptRunsBean.java    License: Apache License 2.0 4 votes vote down vote up
public synchronized void onChangeMatrixAdapter(MatrixAdapter matrixAdapter){
    logger.debug("onChangeMatrixAdapter started with {}", matrixAdapter);
    Collections.replaceAll(matrixAdapterList, matrixAdapter, matrixAdapter);
}
 
Example 18
Source Project: styx   File: HandlerUtil.java    License: Apache License 2.0 4 votes vote down vote up
static List<String> argsReplace(List<String> template, String parameter) {
  List<String> result = new ArrayList<>(template);

  Collections.replaceAll(result, "{}", parameter);
  return result;
}
 
Example 19
/**
   Replace a node ID in this way.
   @param oldID The old node ID.
   @param newID The new node ID.
*/
public void replace(Long oldID, Long newID) {
    Collections.replaceAll(ids, oldID, newID);
}