Java Code Examples for com.google.common.collect.Lists#newArrayListWithCapacity()

The following examples show how to use com.google.common.collect.Lists#newArrayListWithCapacity() . 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
Source File: DefaultMessageManageService.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
@Override
public List<BrokerMessageInfo> getPartitionMessage(String topic, String app, short partition, long index, int count) {
    try {
        List<BrokerMessage> brokerMessages = Lists.newArrayListWithCapacity(count);
        List<BrokerMessageInfo> result = Lists.newArrayListWithCapacity(count);
        byte[][] bytes = storeManagementService.readMessages(topic, partition, index, count);
        if (ArrayUtils.isNotEmpty(bytes)) {
            for (byte[] message : bytes) {
                brokerMessages.add(Serializer.readBrokerMessage(ByteBuffer.wrap(message)));
            }
        }

        brokerMessages = messageConvertSupport.convert(brokerMessages, SourceType.INTERNAL.getValue());
        for (BrokerMessage brokerMessage : brokerMessages) {
            result.add(new BrokerMessageInfo(brokerMessage));
        }
        return result;
    } catch (Exception e) {
        throw new ManageException(e);
    }
}
 
Example 2
Source File: ImhotepClient.java    From imhotep with Apache License 2.0 6 votes vote down vote up
public List<ShardIdWithVersion> getShardListWithVersion(final String dataset, final ShardFilter filterFunc) {
    final Map<Host, List<DatasetInfo>> shardListMap = getShardList();

    final Map<String,Long> latestVersionMap = new HashMap<String, Long>();
    for (final List<DatasetInfo> datasetList : shardListMap.values()) {
        for (final DatasetInfo datasetInfo : datasetList) {
            for (final ShardInfo shard : datasetInfo.getShardList()) {
                if (dataset.equals(shard.dataset) && filterFunc.accept(shard)) {
                    //is in time range, check version
                    if(!latestVersionMap.containsKey(shard.shardId) || latestVersionMap.get(shard.shardId) < shard.version) {
                        latestVersionMap.put(shard.shardId, shard.version);
                    }
                }
            }
        }
    }

    final List<ShardIdWithVersion> ret = Lists.newArrayListWithCapacity(latestVersionMap.size());
    for (final Map.Entry<String, Long> e : latestVersionMap.entrySet()) {
        ret.add(new ShardIdWithVersion(e.getKey(), e.getValue()));
    }
    Collections.sort(ret);
    return ret;
}
 
Example 3
Source File: ServiceManager.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
ImmutableMap<Service, Long> startupTimes() {
  List<Entry<Service, Long>> loadTimes;
  monitor.enter();
  try {
    loadTimes = Lists.newArrayListWithCapacity(startupTimers.size());
    // N.B. There will only be an entry in the map if the service has started
    for (Entry<Service, Stopwatch> entry : startupTimers.entrySet()) {
      Service service = entry.getKey();
      Stopwatch stopWatch = entry.getValue();
      if (!stopWatch.isRunning() && !(service instanceof NoOpService)) {
        loadTimes.add(Maps.immutableEntry(service, stopWatch.elapsed(MILLISECONDS)));
      }
    }
  } finally {
    monitor.leave();
  }
  Collections.sort(loadTimes, Ordering.natural().onResultOf(new Function<Entry<Service, Long>, Long>() {
                                                              @Override
                                                              public Long apply(Map.Entry<Service, Long> input) {
                                                                return input.getValue();
                                                              }
                                                            }));
  return ImmutableMap.copyOf(loadTimes);
}
 
Example 4
Source File: DefaultPullConsumer.java    From qmq with Apache License 2.0 6 votes vote down vote up
private void doPull(PullMessageFuture request) {
    List<Message> messages = Lists.newArrayListWithCapacity(request.getFetchSize());
    try {
        retryPullEntry.pull(request.getFetchSize(), request.getTimeout(), messages);
        if (messages.size() > 0 && request.isPullOnce()) return;

        if (request.isResetCreateTime()) {
            request.resetCreateTime();
        }

        do {
            int fetchSize = request.getFetchSize() - messages.size();
            if (fetchSize <= 0) break;
            PlainPullEntry.PlainPullResult result = pullEntry.pull(fetchSize, request.getTimeout(), messages);
            if (result == PlainPullEntry.PlainPullResult.NO_BROKER) {
                break;
            }
        } while (messages.size() < request.getFetchSize() && !request.isExpired());
    } catch (Exception e) {
        LOGGER.error("DefaultPullConsumer doPull exception. subject={}, group={}", subject(), group(), e);
        Metrics.counter("qmq_pull_defaultPull_doPull_fail", SUBJECT_GROUP_ARRAY, new String[]{subject(), group()}).inc();
    } finally {
        setResult(request, messages);
    }
}
 
Example 5
Source File: TaskAttemptImpl.java    From incubator-tez with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
protected void sendInputFailedToConsumers() {
  Vertex vertex = getVertex();
  Map<Vertex, Edge> edges = vertex.getOutputVertices();
  if (edges != null && !edges.isEmpty()) {
    List<TezEvent> tezIfEvents = Lists.newArrayListWithCapacity(edges.size());
    for (Vertex edgeVertex : edges.keySet()) {
      tezIfEvents.add(new TezEvent(new InputFailedEvent(), 
          new EventMetaData(EventProducerConsumerType.SYSTEM, 
              vertex.getName(), 
              edgeVertex.getName(), 
              getID())));
    }
    sendEvent(new VertexEventRouteEvent(vertex.getVertexId(), tezIfEvents));
  }
}
 
Example 6
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
/**
 * @param unnormalizedID ID value that determines partition
 */
private Iterable<HostAndPort> choosePartitionAndReplicas(long unnormalizedID) {
  List<HostAndPort> replicas = partitions.get(LangUtils.mod(unnormalizedID, partitions.size()));
  int numReplicas = replicas.size();
  if (numReplicas <= 1) {
    return replicas;
  }
  // Fix first replica; cycle through remainder in order since the remainder doesn't matter
  int currentReplica = LangUtils.mod(RandomUtils.md5HashToLong(unnormalizedID), numReplicas);
  Collection<HostAndPort> rotatedReplicas = Lists.newArrayListWithCapacity(numReplicas);
  for (int i = 0; i < numReplicas; i++) {
    rotatedReplicas.add(replicas.get(currentReplica));
    if (++currentReplica == numReplicas) {
      currentReplica = 0;
    }
  }
  return rotatedReplicas;
}
 
Example 7
Source File: DimensionalConfigurationSchema.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * This is a helper method which converts the given {@link JSONArray} to a {@link List} of Strings.
 *
 * @param jsonStringArray The {@link JSONArray} to convert.
 * @return The converted {@link List} of Strings.
 */
//TODO To be removed when Malhar Library 3.3 becomes a dependency.
private List<String> getStringsFromJSONArray(JSONArray jsonStringArray) throws JSONException
{
  List<String> stringArray = Lists.newArrayListWithCapacity(jsonStringArray.length());

  for (int stringIndex = 0; stringIndex < jsonStringArray.length(); stringIndex++) {
    stringArray.add(jsonStringArray.getString(stringIndex));
  }

  return stringArray;
}
 
Example 8
Source File: TeamModule.java    From UHC with MIT License 5 votes vote down vote up
@Override
public void initialize() throws InvalidConfigurationException {
    if (!config.contains(REMOVED_COMBOS_KEY)) {
        config.set(REMOVED_COMBOS_KEY, Lists.newArrayList(
                "RESET",
                "STRIKETHROUGH",
                "MAGIC",
                "BLACK",
                "WHITE",
                "=GRAY+ITALIC" // spectator styling in tab
        ));
    }

    Predicate<Prefix> isFiltered;
    try {
        final List<String> removedCombos = config.getStringList(REMOVED_COMBOS_KEY);
        final List<Predicate<Prefix>> temp = Lists.newArrayListWithCapacity(removedCombos.size());

        final PrefixColourPredicateConverter converter = new PrefixColourPredicateConverter();
        for (final String combo : removedCombos) {
            temp.add(converter.convert(combo));
        }

        isFiltered = Predicates.or(temp);
    } catch (Exception ex) {
        ex.printStackTrace();
        plugin.getLogger().severe("Failed to parse filtered team combos, allowing all combos to be used instead");
        isFiltered = Predicates.alwaysFalse();
    }

    setupTeams(Predicates.not(isFiltered));
    this.icon.setLore(messages.evalTemplates("lore", ImmutableMap.of("count", teams.size())));
}
 
Example 9
Source File: AsyncDispatcherConcurrent.java    From tez with Apache License 2.0 5 votes vote down vote up
AsyncDispatcherConcurrent(String name, int numThreads) {
  super(name);
  Preconditions.checkArgument(numThreads > 0);
  this.name = name;
  this.eventQueues = Lists.newArrayListWithCapacity(numThreads);
  this.numThreads = numThreads;
}
 
Example 10
Source File: VertexDataMovementEventsGeneratedEvent.java    From incubator-tez with Apache License 2.0 5 votes vote down vote up
public void fromProto(VertexDataMovementEventsGeneratedProto proto) {
  this.vertexID = TezVertexID.fromString(proto.getVertexId());
  int eventCount = proto.getTezDataMovementEventCount();
  if (eventCount > 0) {
    this.events = Lists.newArrayListWithCapacity(eventCount);
  }
  for (TezDataMovementEventProto eventProto :
      proto.getTezDataMovementEventList()) {
    Event evt = null;
    if (eventProto.hasCompositeDataMovementEvent()) {
      evt = ProtoConverters.convertCompositeDataMovementEventFromProto(
          eventProto.getCompositeDataMovementEvent());
    } else if (eventProto.hasDataMovementEvent()) {
      evt = ProtoConverters.convertDataMovementEventFromProto(
          eventProto.getDataMovementEvent());
    } else if (eventProto.hasRootInputDataInformationEvent()) {
      evt = ProtoConverters.convertRootInputDataInformationEventFromProto(
          eventProto.getRootInputDataInformationEvent());
    }
    EventMetaData sourceInfo = null;
    EventMetaData destinationInfo = null;
    if (eventProto.hasSourceInfo()) {
      sourceInfo = convertEventMetaDataFromProto(eventProto.getSourceInfo());
    }
    if (eventProto.hasDestinationInfo()) {
      destinationInfo = convertEventMetaDataFromProto(eventProto.getDestinationInfo());
    }
    TezEvent tezEvent = new TezEvent(evt, sourceInfo);
    tezEvent.setDestinationInfo(destinationInfo);
    this.events.add(tezEvent);
  }
}
 
Example 11
Source File: SingularityDeployChecker.java    From Singularity with Apache License 2.0 5 votes vote down vote up
private List<SingularityTask> getTasks(
  Collection<SingularityTaskId> taskIds,
  Map<SingularityTaskId, SingularityTask> taskIdToTask
) {
  final List<SingularityTask> tasks = Lists.newArrayListWithCapacity(taskIds.size());

  for (SingularityTaskId taskId : taskIds) {
    // TODO what if one is missing?
    tasks.add(taskIdToTask.get(taskId));
  }

  return tasks;
}
 
Example 12
Source File: MessageProducerInner.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public List<PartitionMetadata> getBrokerPartitions(TopicMetadata topicMetadata, List<BrokerNode> brokerNodes) {
    if (topicMetadata.getBrokers().equals(brokerNodes)) {
        return topicMetadata.getPartitions();
    }
    List<PartitionMetadata> result = Lists.newArrayListWithCapacity(topicMetadata.getPartitions().size());
    for (BrokerNode brokerNode : brokerNodes) {
        List<PartitionMetadata> brokerPartitions = topicMetadata.getBrokerPartitions(brokerNode.getId());
        if (brokerPartitions != null) {
            result.addAll(brokerPartitions);
        }
    }
    return result;
}
 
Example 13
Source File: GuiComponentTankLevel.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
public void renderOverlay(int offsetX, int offsetY, int mouseX, int mouseY) {
	if (fluidStack != null && isMouseOver(mouseX, mouseY)) {
		final List<String> lines = Lists.newArrayListWithCapacity(2);
		if (displayFluidName) {
			final String translatedFluidName = MiscUtils.getTranslatedFluidName(fluidStack);
			if (!Strings.isNullOrEmpty(translatedFluidName))
				lines.add(translatedFluidName);
		}

		lines.add(String.format("%d/%d", fluidStack.amount, capacity));
		parent.drawHoveringText(lines, offsetX + mouseX, offsetY + mouseY);
	}
}
 
Example 14
Source File: WsaDecoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@Override
public List<WsaHeader> decode(List<SOAPHeaderElement> list) {
    List<WsaHeader> wsaHeaders = Lists.newArrayListWithCapacity(list.size());
    boolean to = false;
    boolean replyTo = false;
    boolean messageId = false;
    boolean action = false;
    for (SOAPHeaderElement soapHeaderElement : list) {
        if (soapHeaderElement.getLocalName().equals(WsaConstants.EN_TO)) {
            wsaHeaders.add(new WsaToHeader(soapHeaderElement.getValue()));
            to = true;
        } else if (soapHeaderElement.getLocalName().equals(WsaConstants.EN_ACTION)) {
            wsaHeaders.add(new WsaActionHeader(soapHeaderElement.getValue()));
            action = true;
        } else if (soapHeaderElement.getLocalName().equals(WsaConstants.EN_REPLY_TO)) {
            Iterator<?> iter = soapHeaderElement.getChildElements();
            while (iter.hasNext()) {
                Node node = (Node) iter.next();
                if (node.getLocalName() != null && node.getLocalName().equals(WsaConstants.EN_ADDRESS)) {
                    wsaHeaders.add(new WsaReplyToHeader(node.getValue()));
                    replyTo = true;
                }
            }
        } else if (soapHeaderElement.getLocalName().equals(WsaConstants.EN_MESSAGE_ID)) {
            wsaHeaders.add(new WsaMessageIDHeader(soapHeaderElement.getValue()));
            messageId = true;
        }
    }
    if ((to || replyTo || messageId) && !action) {
        wsaHeaders.add(new WsaActionHeader(WsaConstants.WSA_FAULT_ACTION));
    }
    return wsaHeaders;
}
 
Example 15
Source File: RunLengthCompressedColumnWriter.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public RunLengthCompressedColumnWriter(int valLen, int rowCnt, int compressBlockSize, OutputStream output) {
    this.valLen = valLen;
    this.numValInBlock = compressBlockSize / valLen;
    this.blockCnt = rowCnt / numValInBlock;
    if (rowCnt % numValInBlock != 0) {
        blockCnt++;
    }
    this.writeBuffer = ByteBuffer.allocate(numValInBlock * (valLen + 8) + 4);
    this.dataOutput = new DataOutputStream(output);
    this.blockDataWriter = new GeneralColumnDataWriter(blockCnt, dataOutput);
    this.entryIndex = Lists.newArrayListWithCapacity(512);
}
 
Example 16
Source File: ProjectL2Cache.java    From Kylin with Apache License 2.0 5 votes vote down vote up
public List<IRealization> getOnlineRealizationByFactTable(String project, String factTable) {
    Set<IRealization> realizations = getRealizationsByTable(project, factTable);
    List<IRealization> result = Lists.newArrayListWithCapacity(realizations.size());
    for (IRealization r : realizations) {
        if (r.getFactTable().equalsIgnoreCase(factTable) && r.isReady()) {
            result.add(r);
        }
    }
    return result;
}
 
Example 17
Source File: AclStorage.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Reads the existing ACL of an inode.  This method always returns the full
 * logical ACL of the inode after reading relevant data from the inode's
 * {@link FsPermission} and {@link AclFeature}.  Note that every inode
 * logically has an ACL, even if no ACL has been set explicitly.  If the inode
 * does not have an extended ACL, then the result is a minimal ACL consising of
 * exactly 3 entries that correspond to the owner, group and other permissions.
 * This method always reads the inode's current state and does not support
 * querying by snapshot ID.  This is because the method is intended to support
 * ACL modification APIs, which always apply a delta on top of current state.
 *
 * @param inode INode to read
 * @return List<AclEntry> containing all logical inode ACL entries
 */
public static List<AclEntry> readINodeLogicalAcl(INode inode) {
  FsPermission perm = inode.getFsPermission();
  AclFeature f = inode.getAclFeature();
  if (f == null) {
    return AclUtil.getMinimalAcl(perm);
  }

  final List<AclEntry> existingAcl;
  // Split ACL entries stored in the feature into access vs. default.
  List<AclEntry> featureEntries = getEntriesFromAclFeature(f);
  ScopedAclEntries scoped = new ScopedAclEntries(featureEntries);
  List<AclEntry> accessEntries = scoped.getAccessEntries();
  List<AclEntry> defaultEntries = scoped.getDefaultEntries();

  // Pre-allocate list size for the explicit entries stored in the feature
  // plus the 3 implicit entries (owner, group and other) from the permission
  // bits.
  existingAcl = Lists.newArrayListWithCapacity(featureEntries.size() + 3);

  if (!accessEntries.isEmpty()) {
    // Add owner entry implied from user permission bits.
    existingAcl.add(new AclEntry.Builder().setScope(AclEntryScope.ACCESS)
        .setType(AclEntryType.USER).setPermission(perm.getUserAction())
        .build());

    // Next add all named user and group entries taken from the feature.
    existingAcl.addAll(accessEntries);

    // Add mask entry implied from group permission bits.
    existingAcl.add(new AclEntry.Builder().setScope(AclEntryScope.ACCESS)
        .setType(AclEntryType.MASK).setPermission(perm.getGroupAction())
        .build());

    // Add other entry implied from other permission bits.
    existingAcl.add(new AclEntry.Builder().setScope(AclEntryScope.ACCESS)
        .setType(AclEntryType.OTHER).setPermission(perm.getOtherAction())
        .build());
  } else {
    // It's possible that there is a default ACL but no access ACL. In this
    // case, add the minimal access ACL implied by the permission bits.
    existingAcl.addAll(AclUtil.getMinimalAcl(perm));
  }

  // Add all default entries after the access entries.
  existingAcl.addAll(defaultEntries);

  // The above adds entries in the correct order, so no need to sort here.
  return existingAcl;
}
 
Example 18
Source File: FSDirXAttrOp.java    From hadoop with Apache License 2.0 4 votes vote down vote up
static List<XAttr> getXAttrs(FSDirectory fsd, final String srcArg,
                             List<XAttr> xAttrs)
    throws IOException {
  String src = srcArg;
  checkXAttrsConfigFlag(fsd);
  FSPermissionChecker pc = fsd.getPermissionChecker();
  final boolean isRawPath = FSDirectory.isReservedRawName(src);
  boolean getAll = xAttrs == null || xAttrs.isEmpty();
  if (!getAll) {
    XAttrPermissionFilter.checkPermissionForApi(pc, xAttrs, isRawPath);
  }
  byte[][] pathComponents = FSDirectory.getPathComponentsForReservedPath(src);
  src = fsd.resolvePath(pc, src, pathComponents);
  final INodesInPath iip = fsd.getINodesInPath(src, true);
  if (fsd.isPermissionEnabled()) {
    fsd.checkPathAccess(pc, iip, FsAction.READ);
  }
  List<XAttr> all = FSDirXAttrOp.getXAttrs(fsd, src);
  List<XAttr> filteredAll = XAttrPermissionFilter.
      filterXAttrsForApi(pc, all, isRawPath);

  if (getAll) {
    return filteredAll;
  }
  if (filteredAll == null || filteredAll.isEmpty()) {
    return null;
  }
  List<XAttr> toGet = Lists.newArrayListWithCapacity(xAttrs.size());
  for (XAttr xAttr : xAttrs) {
    boolean foundIt = false;
    for (XAttr a : filteredAll) {
      if (xAttr.getNameSpace() == a.getNameSpace() && xAttr.getName().equals(
          a.getName())) {
        toGet.add(a);
        foundIt = true;
        break;
      }
    }
    if (!foundIt) {
      throw new IOException(
          "At least one of the attributes provided was not found.");
    }
  }
  return toGet;
}
 
Example 19
Source File: Stack.java    From OpenModsLib with MIT License 4 votes vote down vote up
public Stack(int initialCapacity) {
	this.data = Lists.newArrayListWithCapacity(initialCapacity);
	this.bottomElement = 0;
}
 
Example 20
Source File: Closure_107_CommandLineRunner_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Users may specify JS inputs via the legacy {@code --js} option, as well
 * as via additional arguments to the Closure Compiler. For example, it is
 * convenient to leverage the additional arguments feature when using the
 * Closure Compiler in combination with {@code find} and {@code xargs}:
 * <pre>
 * find MY_JS_SRC_DIR -name '*.js' \
 *     | xargs java -jar compiler.jar --manage_closure_dependencies
 * </pre>
 * The {@code find} command will produce a list of '*.js' source files in
 * the {@code MY_JS_SRC_DIR} directory while {@code xargs} will convert them
 * to a single, space-delimited set of arguments that are appended to the
 * {@code java} command to run the Compiler.
 * <p>
 * Note that it is important to use the
 * {@code --manage_closure_dependencies} option in this case because the
 * order produced by {@code find} is unlikely to be sorted correctly with
 * respect to {@code goog.provide()} and {@code goog.requires()}.
 */
List<String> getJsFiles() {
  List<String> allJsInputs = Lists.newArrayListWithCapacity(
      js.size() + arguments.size());
  allJsInputs.addAll(js);
  allJsInputs.addAll(arguments);
  return allJsInputs;
}