Exemple #1
0
 public static <R, I, C extends Collection<R>> C collect(
     Iterable<? extends I> source, C destination, Transformer<R, I> transformer) {
   for (I item : source) {
     destination.add(transformer.transform(item));
   }
   return destination;
 }
Exemple #2
0
 void render(
     PerformanceTestHistory testHistory,
     Transformer<String, MeasuredOperationList> valueRenderer,
     PrintWriter out) {
   List<? extends PerformanceTestExecution> sortedResults =
       Lists.reverse(testHistory.getExecutions());
   out.println("  [");
   List<String> labels = testHistory.getScenarioLabels();
   for (int i = 0; i < labels.size(); i++) {
     if (i > 0) {
       out.println(",");
     }
     out.println("  {");
     out.println("    \"label\": \"" + labels.get(i) + "\",");
     out.print("\"data\": [");
     boolean empty = true;
     for (int j = 0; j < sortedResults.size(); j++) {
       PerformanceTestExecution results = sortedResults.get(j);
       MeasuredOperationList measuredOperations = results.getScenarios().get(i);
       if (!measuredOperations.isEmpty()) {
         if (!empty) {
           out.print(", ");
         }
         out.print("[" + j + ", " + valueRenderer.transform(measuredOperations) + "]");
         empty = false;
       }
     }
     out.println("]");
     out.print("  }");
   }
   out.println();
   out.println("]");
 }
Exemple #3
0
 public static <R, I> R[] collectArray(I[] list, R[] destination, Transformer<R, I> transformer) {
   assert list.length <= destination.length;
   for (int i = 0; i < list.length; ++i) {
     destination[i] = transformer.transform(list[i]);
   }
   return destination;
 }
 public static <K, V> void collectMap(
     Map<K, V> destination,
     Iterable<? extends V> items,
     Transformer<? extends K, ? super V> keyGenerator) {
   for (V item : items) {
     destination.put(keyGenerator.transform(item), item);
   }
 }
  public static <K, V> ImmutableListMultimap<K, V> groupBy(
      Iterable<? extends V> iterable, Transformer<? extends K, V> grouper) {
    ImmutableListMultimap.Builder<K, V> builder = ImmutableListMultimap.builder();

    for (V element : iterable) {
      K key = grouper.transform(element);
      builder.put(key, element);
    }

    return builder.build();
  }
 public static <E> boolean replace(
     List<E> list, Spec<? super E> filter, Transformer<? extends E, ? super E> transformer) {
   boolean replaced = false;
   int i = 0;
   for (E it : list) {
     if (filter.isSatisfiedBy(it)) {
       list.set(i, transformer.transform(it));
       replaced = true;
     }
     ++i;
   }
   return replaced;
 }
 public WorkResult execute(WindowsResourceCompileSpec spec) {
   boolean didWork = false;
   boolean windowsPathLimitation = OperatingSystem.current().isWindows();
   MutableCommandLineToolInvocation invocation = baseInvocation.copy();
   spec = specTransformer.transform(spec);
   for (File sourceFile : spec.getSourceFiles()) {
     RcCompilerArgsTransformer argsTransformer =
         new RcCompilerArgsTransformer(sourceFile, windowsPathLimitation);
     invocation.setArgs(argsTransformer.transform(spec));
     invocation.setWorkDirectory(spec.getObjectFileDir());
     WorkResult result = commandLineTool.execute(invocation);
     didWork |= result.getDidWork();
   }
   return new SimpleWorkResult(didWork);
 }
Exemple #8
0
  public static <K, V> Map<K, List<V>> groupBy(
      Iterable<? extends V> iterable, Transformer<? extends K, V> grouper) {
    Map<K, List<V>> map = new LinkedHashMap<K, List<V>>();

    for (V element : iterable) {
      K key = grouper.transform(element);
      List<V> entries = map.get(key);
      if (entries == null) {
        entries = new LinkedList<V>();
        map.put(key, entries);
      }

      entries.add(element);
    }

    return map;
  }
Exemple #9
0
  public ClassLoaderDetails getDetails(
      ClassLoader classLoader, Transformer<ClassLoaderDetails, ClassLoader> factory) {
    lock.lock();
    try {
      ClassLoaderDetails details = classLoaderDetails.getIfPresent(classLoader);
      if (details != null) {
        return details;
      }

      details = factory.transform(classLoader);
      classLoaderDetails.put(classLoader, details);
      classLoaderIds.put(details.uuid, classLoader);
      return details;
    } finally {
      lock.unlock();
    }
  }
Exemple #10
0
  public ClassLoader getClassLoader(
      ClassLoaderDetails details, Transformer<ClassLoader, ClassLoaderDetails> factory) {
    lock.lock();
    try {
      ClassLoader classLoader = classLoaderIds.getIfPresent(details.uuid);
      if (classLoader != null) {
        return classLoader;
      }

      classLoader = factory.transform(details);
      classLoaderIds.put(details.uuid, classLoader);
      classLoaderDetails.put(classLoader, details);
      return classLoader;
    } finally {
      lock.unlock();
    }
  }
  protected void transformArgs(List<String> input, List<String> output, File tempDir) {
    GFileUtils.mkdirs(tempDir);
    File optionsFile = new File(tempDir, "options.txt");
    try {
      PrintWriter writer = new PrintWriter(optionsFile);
      try {
        ArgWriter argWriter = argWriterFactory.transform(writer);
        argWriter.args(input);
      } finally {
        IOUtils.closeQuietly(writer);
      }
    } catch (IOException e) {
      throw new UncheckedIOException(
          String.format(
              "Could not write compiler options file '%s'.", optionsFile.getAbsolutePath()),
          e);
    }

    output.add(String.format("@%s", optionsFile.getAbsolutePath()));
  }
  public void resolve(DependencyMetaData dependency, BuildableComponentResolveResult result) {
    ModuleVersionSelector requested = dependency.getRequested();
    LOGGER.debug("Attempting to resolve {} using repositories {}", requested, repositoryNames);
    ModuleComponentIdentifier moduleComponentIdentifier =
        new DefaultModuleComponentIdentifier(
            requested.getGroup(), requested.getName(), requested.getVersion());
    ModuleVersionIdentifier moduleVersionIdentifier =
        new DefaultModuleVersionIdentifier(
            requested.getGroup(), requested.getName(), requested.getVersion());

    List<Throwable> errors = new ArrayList<Throwable>();

    List<ComponentMetaDataResolveState> resolveStates =
        new ArrayList<ComponentMetaDataResolveState>();
    for (ModuleComponentRepository repository : repositories) {
      resolveStates.add(
          new ComponentMetaDataResolveState(
              dependency, moduleComponentIdentifier, repository, componentChooser));
    }

    final RepositoryChainModuleResolution latestResolved = findBestMatch(resolveStates, errors);
    if (latestResolved != null) {
      LOGGER.debug("Using {} from {}", latestResolved.module.getId(), latestResolved.repository);
      for (Throwable error : errors) {
        LOGGER.debug("Discarding resolve failure.", error);
      }

      result.resolved(metaDataFactory.transform(latestResolved));
      return;
    }
    if (!errors.isEmpty()) {
      result.failed(new ModuleVersionResolveException(moduleComponentIdentifier, errors));
    } else {
      for (ComponentMetaDataResolveState resolveState : resolveStates) {
        resolveState.applyTo(result);
      }
      result.notFound(moduleVersionIdentifier);
    }
  }
 public void execute(FileCopyDetails fileCopyDetails) {
   RelativePath path = fileCopyDetails.getRelativePath();
   path = path.replaceLastName(transformer.transform(path.getLastName()));
   fileCopyDetails.setRelativePath(path);
 }
Exemple #14
0
  private void init() throws Exception {
    if (localAddresses != null) {
      return;
    }

    Transformer<Boolean, NetworkInterface> loopback = loopback();
    Transformer<Boolean, NetworkInterface> multicast = multicast();

    localAddresses = new ArrayList<InetAddress>();
    remoteAddresses = new ArrayList<InetAddress>();
    multicastInterfaces = new ArrayList<NetworkInterface>();

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
      NetworkInterface networkInterface = interfaces.nextElement();
      LOGGER.debug(
          "Adding IP addresses for network interface {}", networkInterface.getDisplayName());
      try {
        Boolean isLoopbackInterface = loopback.transform(networkInterface);
        LOGGER.debug("Is this a loopback interface? {}", isLoopbackInterface);
        Boolean isMulticast = multicast.transform(networkInterface);
        LOGGER.debug("Is this a multicast interface? {}", isMulticast);
        boolean isRemote = false;

        Enumeration<InetAddress> candidates = networkInterface.getInetAddresses();
        while (candidates.hasMoreElements()) {
          InetAddress candidate = candidates.nextElement();
          if (isLoopbackInterface == null) {
            // Don't know if this is a loopback interface or not
            if (candidate.isLoopbackAddress()) {
              LOGGER.debug("Adding loopback address {}", candidate);
              localAddresses.add(candidate);
            } else {
              LOGGER.debug("Adding remote address {}", candidate);
              remoteAddresses.add(candidate);
              isRemote = true;
            }
          } else if (isLoopbackInterface) {
            if (candidate.isLoopbackAddress()) {
              LOGGER.debug("Adding loopback address {}", candidate);
              localAddresses.add(candidate);
            } else {
              LOGGER.debug("Ignoring remote address on loopback interface {}", candidate);
            }
          } else {
            if (candidate.isLoopbackAddress()) {
              LOGGER.debug("Ignoring loopback address on remote interface {}", candidate);
            } else {
              LOGGER.debug("Adding remote address {}", candidate);
              remoteAddresses.add(candidate);
              isRemote = true;
            }
          }
        }

        if (!Boolean.FALSE.equals(isMulticast)) {
          // Prefer remotely reachable interfaces over loopback interfaces for multicast
          if (isRemote) {
            LOGGER.debug("Adding remote multicast interface {}", networkInterface.getDisplayName());
            multicastInterfaces.add(0, networkInterface);
          } else {
            LOGGER.debug(
                "Adding loopback multicast interface {}", networkInterface.getDisplayName());
            multicastInterfaces.add(networkInterface);
          }
        }
      } catch (Throwable e) {
        throw new RuntimeException(
            String.format(
                "Could not determine the IP addresses for network interface %s",
                networkInterface.getName()),
            e);
      }
    }

    if (localAddresses.isEmpty()) {
      InetAddress fallback = InetAddress.getByName(null);
      LOGGER.debug("No loopback addresses, using fallback {}", fallback);
      localAddresses.add(fallback);
    }
    if (remoteAddresses.isEmpty()) {
      InetAddress fallback = InetAddress.getLocalHost();
      LOGGER.debug("No remote addresses, using fallback {}", fallback);
      remoteAddresses.add(fallback);
    }
    if (multicastInterfaces.isEmpty()) {
      LOGGER.debug("No multicast interfaces, using fallbacks");
      Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
      while (networkInterfaces.hasMoreElements()) {
        multicastInterfaces.add(networkInterfaces.nextElement());
      }
    }

    // Detect Openshift IP environment variable.
    InetAddress openshiftEnvironment = findOpenshiftAddresses();
    if (openshiftEnvironment != null) {
      localBindingAddress = openshiftEnvironment;
    } else {
      localBindingAddress = InetAddress.getByName("0.0.0.0");
    }
  }