Java Code Examples for com.google.common.collect.MoreCollectors

The following are top voted examples for showing how to use com.google.common.collect.MoreCollectors. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: ios-device-control   File: IosAppInfo.java   View source code 6 votes vote down vote up
/** Returns the application info read from either an app folder or ipa archive */
public static IosAppInfo readFromPath(Path ipaOrAppPath) throws IOException {
  NSObject plistDict;
  if (Files.isDirectory(ipaOrAppPath)) {
    plistDict = PlistParser.fromPath(ipaOrAppPath.resolve("Info.plist"));
  } else {
    try (FileSystem ipaFs = FileSystems.newFileSystem(ipaOrAppPath, null)) {
      Path appPath =
          MoreFiles.listFiles(ipaFs.getPath("Payload"))
              .stream()
              // Can't use Files.isDirectory, because no entry is a "directory" in a zip.
              .filter(e -> e.toString().endsWith(".app/"))
              .collect(MoreCollectors.onlyElement());
      plistDict = PlistParser.fromPath(appPath.resolve("Info.plist"));
    }
  }
  return readFromPlistDictionary((NSDictionary) plistDict);
}
 
Example 2
Project: ios-device-control   File: SimulatorDeviceHost.java   View source code 5 votes vote down vote up
private static IosModel toModel(String deviceType) throws IOException {
  checkArgument(deviceType.matches("[-\\w]*"));
  Path profileInfoBasePath =
      Paths.get(
          "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/"
              + "Developer/Library/CoreSimulator/Profiles/DeviceTypes");
  Path deviceTypePath =
      MoreFiles.listFiles(profileInfoBasePath)
          .stream()
          // The directory name that matches a given device type is the same as the name from the
          // device.plist file, with the hyphens replaced with spaces, hyphens, parentheses or
          // periods
          .filter(
              p -> MoreFiles.getNameWithoutExtension(p).replaceAll("\\W", "-").equals(deviceType))
          .collect(MoreCollectors.onlyElement());
  String rawProductName = MoreFiles.getNameWithoutExtension(deviceTypePath);
  String productName = GENERATION_PATTERN.matcher(rawProductName).replaceFirst("$1");

  Path profilePath = deviceTypePath.resolve("Contents/Resources/profile.plist");
  NSDictionary profileDict = (NSDictionary) PlistParser.fromPath(profilePath);
  String identifier = profileDict.get("modelIdentifier").toString();

  NSArray supportedArchs = (NSArray) profileDict.get("supportedArchs");
  // The supported architecture can either be just i386 or i386 and x86_64. The
  // actual architecture will be x86_64 if its supported, or i386 otherwise.
  Architecture architecture =
      supportedArchs.containsObject(Architecture.X86_64.toString())
          ? Architecture.X86_64
          : Architecture.I386;
  return IosModel.builder()
      .identifier(identifier)
      .productName(productName)
      .architecture(architecture)
      .build();
}
 
Example 3
Project: ios-device-control   File: SimulatorDeviceImpl.java   View source code 5 votes vote down vote up
private ImmutableSet<IosAppInfo> userApps() throws IOException {
  Path appPath =
      Paths.get(
          System.getProperty("user.home"),
          "Library/Developer/CoreSimulator/Devices",
          udid,
          "data/Containers/Bundle/Application");
  if (!Files.exists(appPath)) {
    return ImmutableSet.of();
  }
  try {
    return MoreFiles.listFiles(appPath)
        .stream()
        .map(
            p ->
                tunnel(
                    () ->
                        MoreFiles.listFiles(p)
                            .stream()
                            .filter(a -> MoreFiles.getFileExtension(a).equals("app"))
                            .collect(MoreCollectors.onlyElement())))
        .map(a -> tunnel(() -> IosAppInfo.readFromPath(a)))
        .collect(toImmutableSet());
  } catch (TunnelException e) {
    throw e.getCauseAs(IOException.class);
  }
}
 
Example 4
Project: orchestrator   File: AbstractMesosDeploymentService.java   View source code 5 votes vote down vote up
protected NodeTemplate getHostNode(DirectedMultigraph<NodeTemplate, RelationshipTemplate> graph,
    NodeTemplate taskNode) {
  return graph
      .incomingEdgesOf(taskNode)
      .stream()
      .filter(
          relationship -> HOST_CAPABILITY_NAME.equals(relationship.getTargetedCapabilityName()))
      .map(graph::getEdgeSource)
      // if more than 1 node is present -> IllegalArgumentException
      .collect(MoreCollectors.toOptional())
      .orElseThrow(() -> new IllegalArgumentException(
          String.format("No hosting node provided for node <%s>", taskNode.getName())));
}
 
Example 5
Project: error-prone   File: TypeParameterShadowing.java   View source code 5 votes vote down vote up
private TypeParameterTree typeParameterInList(
    List<? extends TypeParameterTree> typeParameters, TypeVariableSymbol v) {
  return typeParameters
      .stream()
      .filter(t -> t.getName().contentEquals(v.name))
      .collect(MoreCollectors.onlyElement());
}
 
Example 6
Project: orchestrator   File: MarathonServiceImpl.java   View source code 4 votes vote down vote up
@Override
public void finalizeDeploy(DeploymentMessage deploymentMessage) {
  Deployment deployment = getDeployment(deploymentMessage);

  ArchiveRoot ar = toscaService
      .prepareTemplate(deployment.getTemplate(), deployment.getParameters());

  Map<String, OutputDefinition> outputs = Optional
      .ofNullable(ar.getTopology())
      .map(Topology::getOutputs)
      .orElseGet(HashMap::new);
  if (!outputs.isEmpty()) {
    String groupId = deployment.getId();
    Group group = getMarathonClient(deployment).getGroup(groupId);

    Map<String, NodeTemplate> nodes = Optional
        .ofNullable(ar.getTopology())
        .map(Topology::getNodeTemplates)
        .orElseGet(HashMap::new);

    DirectedMultigraph<NodeTemplate, RelationshipTemplate> graph =
        toscaService.buildNodeGraph(nodes, false);

    TopologicalOrderIterator<NodeTemplate, RelationshipTemplate> orderIterator =
        new TopologicalOrderIterator<>(graph);

    List<NodeTemplate> orderedMarathonApps = CommonUtils
        .iteratorToStream(orderIterator)
        .filter(node -> toscaService.isOfToscaType(node, ToscaConstants.Nodes.MARATHON))
        .collect(Collectors.toList());

    MarathonProperties marathonProperties =
        marathonClientFactory.getFrameworkProperties(deployment);
    RuntimeProperties runtimeProperties = new RuntimeProperties();
    for (NodeTemplate marathonNode : orderedMarathonApps) {

      runtimeProperties.put(marathonProperties.getLoadBalancerIps(), marathonNode.getName(),
          "load_balancer_ips");

      List<Integer> ports = group
          .getApps()
          .stream()
          .filter(
              app -> app.getId().endsWith("/" + marathonNode.getName()))
          .collect(MoreCollectors.toOptional())
          .map(App::getPorts)
          .orElseGet(ArrayList::new);

      NodeTemplate hostNode = getHostNode(graph, marathonNode);
      for (int i = 0; i < ports.size(); ++i) {
        runtimeProperties.put(ports.get(i), hostNode.getName(), "host",
            "publish_ports", String.valueOf(i), "target");
      }
    }
    deployment.setOutputs(indigoInputsPreProcessorService.processOutputs(ar,
        deployment.getParameters(), runtimeProperties));
  }
  super.finalizeDeploy(deploymentMessage);
}