List of usage examples for com.google.common.collect Iterables toArray
static <T> T[] toArray(Iterable<? extends T> iterable, T[] array)
From source file:org.vclipse.vcml.compare.experimental.EMFCompareStructureMergeViewerContentProvider.java
@Override public final Object[] getChildren(Object element) { final Object[] ret; if (element instanceof ComparisonNode) { Comparison target = ((ComparisonNode) element).getTarget(); final Iterable<? extends DifferenceGroup> groups = fViewerGrouper.getGroups(target); if (!isEmpty(groups)) { ret = Iterables.toArray(groups, DifferenceGroup.class); } else {/* w ww . j a v a 2 s .c o m*/ Iterable<ICompareInput> compareInputs = adapt(super.getChildren(((Adapter) element).getTarget()), getAdapterFactory(), ICompareInput.class); ret = toArray(compareInputs, ICompareInput.class); } } else if (element instanceof DifferenceGroup) { Iterable<? extends Diff> differences = ((DifferenceGroup) element).getDifferences(); Iterable<ICompareInput> compareInputs = adapt(differences, getAdapterFactory(), ICompareInput.class); ret = toArray(compareInputs, ICompareInput.class); } else if (element instanceof Adapter) { Iterable<ICompareInput> compareInputs = adapt(super.getChildren(((Adapter) element).getTarget()), getAdapterFactory(), ICompareInput.class); ret = toArray(compareInputs, ICompareInput.class); } else { ret = new Object[0]; } return ret; }
From source file:org.apache.lens.cli.commands.LensCubeCommands.java
/** * Update cube.//w w w .j a v a 2 s. co m * * @param specPair the spec pair * @return the string */ @CliCommand(value = "update cube", help = "update cube") public String updateCube(@CliOption(key = { "", "cube" }, mandatory = true, help = "<cube-name> <path to cube-spec file>") String specPair) { Iterable<String> parts = Splitter.on(' ').trimResults().omitEmptyStrings().split(specPair); String[] pair = Iterables.toArray(parts, String.class); if (pair.length != 2) { return "Syntax error, please try in following " + "format. create fact <fact spec path> <storage spec path>"; } File f = new File(pair[1]); if (!f.exists()) { return "Fact spec path" + f.getAbsolutePath() + " does not exist. Please check the path"; } APIResult result = getClient().updateCube(pair[0], pair[1]); if (result.getStatus() == APIResult.Status.SUCCEEDED) { return "Update of " + pair[0] + " succeeded"; } else { return "Update of " + pair[0] + " failed"; } }
From source file:com.google.cloud.datastore.BaseDatastoreBatchWriter.java
@SuppressWarnings("unchecked") @Override//from w w w .j av a 2 s. co m public final List<Entity> add(FullEntity<?>... entities) { validateActive(); List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.getKey(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (!(key instanceof Key)) { incompleteKeys.add(key); } } Iterator<Key> allocated; if (!incompleteKeys.isEmpty()) { IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class); allocated = getDatastore().allocateId(toAllocate).iterator(); } else { allocated = Collections.emptyIterator(); } List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { if (entity.getKey() instanceof Key) { addInternal((FullEntity<Key>) entity); answer.add(Entity.convert((FullEntity<Key>) entity)); } else { Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build(); addInternal(entityWithAllocatedId); answer.add(entityWithAllocatedId); } } return answer; }
From source file:nz.co.testamation.testcommon.fixture.SomeFixture.java
public static String someString(Iterable<String> choices) { return someString(Iterables.toArray(choices, String.class)); }
From source file:edu.cmu.lti.oaqa.baseqa.providers.ml.classifiers.MekaProvider.java
@Override public boolean initialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams) throws ResourceInitializationException { boolean ret = super.initialize(aSpecifier, aAdditionalParams); // model/*from w ww . j a va 2 s. co m*/ if ((modelFile = new File((String) getParameterValue("model-file"))).exists()) { try { classifier = (MultiLabelClassifier) SerializationHelper.read(modelFile.getAbsolutePath()); } catch (Exception e) { throw new ResourceInitializationException(e); } } // dataset schema if ((datasetSchemaFile = new File((String) getParameterValue("dataset-schema-file"))).exists()) { try { datasetSchema = (Instances) SerializationHelper.read(datasetSchemaFile.getAbsolutePath()); } catch (Exception e) { throw new ResourceInitializationException(e); } } // classifier classifierName = String.class.cast(getParameterValue("classifier-name")); //noinspection unchecked options = Iterables.toArray((Iterable<String>) getParameterValue("options"), String.class); return ret; }
From source file:com.rbmhtechnology.example.japi.querydb.Writer.java
private <T> CompletableFuture<Void> sequence(final Iterable<CompletableFuture<T>> futures) { return CompletableFuture.allOf(Iterables.toArray(futures, CompletableFuture.class)); }
From source file:ch.petikoch.examples.mvvm_rxjava.utils.PreserveFullStackTraceOperator.java
@Override public Subscriber<? super T> call(Subscriber<? super T> child) { Subscriber<T> parent = new Subscriber<T>() { @Override// w ww. j a va 2 s . co m public void onCompleted() { child.onCompleted(); } @Override public void onError(Throwable throwable) { if (Thread.currentThread().getId() != originThreadId) { List<StackTraceElement> originalStackTraceElements = Lists .newArrayList(throwable.getStackTrace()); List<StackTraceElement> additionalAsyncOriginStackTraceElements = asList( asyncOriginStackTraceProvider.getStackTrace()).stream() .filter(stackTraceElement -> !PreserveFullStackTraceOperator.class.getName() .equals(stackTraceElement.getClassName())) .collect(Collectors.toList()); Iterable<StackTraceElement> modifiedStackTraceElements = Iterables .concat(originalStackTraceElements, additionalAsyncOriginStackTraceElements); throwable.setStackTrace(Iterables.toArray(modifiedStackTraceElements, StackTraceElement.class)); } child.onError(throwable); } @Override public void onNext(T t) { child.onNext(t); } }; child.add(parent); return parent; }
From source file:com.facebook.buck.android.GenerateManifestStep.java
@Override public int execute(ExecutionContext context) { if (skeletonManifestPath.getNameCount() == 0) { throw new HumanReadableException("Skeleton manifest filepath is missing"); }// ww w . j a v a 2 s . co m if (outManifestPath.getNameCount() == 0) { throw new HumanReadableException("Output Manifest filepath is missing"); } if (libraryManifestPaths == null || libraryManifestPaths.isEmpty()) { warnUser(context, "No library manifests found. Aborting manifest merge step."); return 1; } try { Files.createParentDirs(outManifestPath.toFile()); } catch (IOException e) { e.printStackTrace(context.getStdErr()); return 1; } List<File> libraryManifestFiles = Lists.newArrayList(); for (Path path : libraryManifestPaths) { libraryManifestFiles.add(path.toFile()); } File skeletonManifestFile = skeletonManifestPath.toFile(); ICallback callback = new ICallback() { @Override public int queryCodenameApiLevel(@NonNull String codename) { return BASE_SDK_LEVEL; } }; IMergerLog log = MergerLog.wrapSdkLog(new BuckEventAndroidLogger(context.getBuckEventBus())); ManifestMerger merger = new ManifestMerger(log, callback); File outManifestFile = outManifestPath.toFile(); if (!merger.process(outManifestFile, skeletonManifestFile, Iterables.toArray(libraryManifestFiles, File.class))) { throw new HumanReadableException("Error generating manifest file"); } if (context.getPlatform() == Platform.WINDOWS) { // Convert line endings to Lf on Windows. try { String xmlText = Files.toString(outManifestFile, Charsets.UTF_8); xmlText = xmlText.replace("\r\n", "\n"); Files.write(xmlText.getBytes(Charsets.UTF_8), outManifestFile); } catch (IOException e) { throw new HumanReadableException("Error converting line endings of manifest file"); } } return 0; }
From source file:org.apache.isis.core.metamodel.facets.CollectionUtils.java
/** * Copies the iterable into the specified type. *///from www. j a v a 2 s.c om public static Object copyOf(final Iterable<Object> iterable, final Class<?> requiredType) { if (iterable == null) { throw new IllegalArgumentException("Iterable must be provided"); } if (requiredType == null) { throw new IllegalArgumentException("RequiredType must be provided"); } // specific list implementations if (CopyOnWriteArrayList.class == requiredType) { return Lists.newCopyOnWriteArrayList(iterable); } if (LinkedList.class == requiredType) { return Lists.newLinkedList(iterable); } if (ArrayList.class == requiredType) { return Lists.newArrayList(iterable); } if (AbstractList.class == requiredType) { return Lists.newArrayList(iterable); } // specific set implementations if (CopyOnWriteArraySet.class == requiredType) { return Sets.newCopyOnWriteArraySet(iterable); } if (LinkedHashSet.class == requiredType) { return Sets.newLinkedHashSet(iterable); } if (HashSet.class == requiredType) { return Sets.newHashSet(iterable); } if (TreeSet.class == requiredType) { Iterable rawIterable = iterable; return Sets.newTreeSet(rawIterable); } if (AbstractSet.class == requiredType) { return Sets.newLinkedHashSet(iterable); } // interfaces if (List.class == requiredType) { return Lists.newArrayList(iterable); } if (SortedSet.class == requiredType) { Iterable rawIterable = iterable; return Sets.newTreeSet(rawIterable); } if (Set.class == requiredType) { return Sets.newLinkedHashSet(iterable); } if (Collection.class == requiredType) { return Lists.newArrayList(iterable); } // array if (requiredType.isArray()) { Class<?> componentType = requiredType.getComponentType(); Iterable rawIterable = iterable; return Iterables.toArray(rawIterable, componentType); } // not recognized return null; }
From source file:com.google.gcloud.datastore.BaseDatastoreBatchWriter.java
@SuppressWarnings("unchecked") @Override//from w w w . j av a 2 s. c o m public final List<Entity> add(FullEntity<?>... entities) { validateActive(); List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { IncompleteKey key = entity.key(); Preconditions.checkArgument(key != null, "Entity must have a key"); if (key instanceof Key) { addInternal((FullEntity<Key>) entity); } else { incompleteKeys.add(key); } } Iterator<Key> allocated; if (!incompleteKeys.isEmpty()) { IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class); allocated = datastore().allocateId(toAllocate).iterator(); } else { allocated = Collections.emptyIterator(); } List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length); for (FullEntity<?> entity : entities) { if (entity.key() instanceof Key) { answer.add(Entity.convert((FullEntity<Key>) entity)); } else { Entity entityWithAllocatedId = Entity.builder(allocated.next(), entity).build(); addInternal(entityWithAllocatedId); answer.add(entityWithAllocatedId); } } return answer; }