Java Code Examples for com.google.common.collect.Iterators#transform()

The following examples show how to use com.google.common.collect.Iterators#transform() . 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: BatchIndexRetrievalImpl.java    From geowave with Apache License 2.0 6 votes vote down vote up
private CloseableIterator<GeoWaveValue[]> getData(final short adapterId, final byte[][] dataIds) {
  final RowReader<GeoWaveRow> rowReader =
      DataIndexUtils.getRowReader(
          operations,
          adapterStore,
          internalAdapterStore,
          fieldSubsets,
          aggregation,
          additionalAuthorizations,
          adapterId,
          dataIds);
  return new CloseableIteratorWrapper<>(
      rowReader,
      Iterators.transform(rowReader, r -> r.getFieldValues()));

}
 
Example 2
Source File: ReflectionClassDef.java    From AppTroy with Apache License 2.0 6 votes vote down vote up
@Nonnull @Override public Set<? extends Field> getFields() {
    return new AbstractSet<Field>() {
        @Nonnull @Override public Iterator<Field> iterator() {
            return Iterators.transform(Iterators.forArray(cls.getDeclaredFields()),
                    new Function<java.lang.reflect.Field, Field>() {
                        @Nullable @Override public Field apply(@Nullable java.lang.reflect.Field input) {
                            return new ReflectionField(input);
                        }
                    });
        }

        @Override public int size() {
            return cls.getDeclaredFields().length;
        }
    };
}
 
Example 3
Source File: TezClientUtils.java    From tez with Apache License 2.0 6 votes vote down vote up
/**
 * Populate {@link Credentials} for the URI's to access them from their {@link FileSystem}s
 * @param uris URIs that need to be accessed
 * @param credentials Credentials object into which to add the credentials
 * @param conf Configuration to access the FileSystem
 * @throws IOException
 */
public static void addFileSystemCredentialsFromURIs(Collection<URI> uris, Credentials credentials,
    Configuration conf) throws IOException {
  // Obtain Credentials for any paths that the user may have configured.
  if (uris != null && !uris.isEmpty()) {
    Iterator<Path> pathIter = Iterators.transform(uris.iterator(), new Function<URI, Path>() {
      @Override
      public Path apply(URI input) {
        return new Path(input);
      }
    });

    Path[] paths = Iterators.toArray(pathIter, Path.class);
    TokenCache.obtainTokensForFileSystems(credentials, paths, conf);
  }
}
 
Example 4
Source File: IterableCopyableDatasetImpl.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
private static Iterator<FileSet<CopyEntity>> partitionCopyableFiles(Dataset dataset,
    Collection<? extends CopyEntity> files) {
  Map<String, FileSet.Builder<CopyEntity>> partitionBuildersMaps = Maps.newHashMap();
  for (CopyEntity file : files) {
    if (!partitionBuildersMaps.containsKey(file.getFileSet())) {
      partitionBuildersMaps.put(file.getFileSet(), new FileSet.Builder<>(file.getFileSet(), dataset));
    }
    partitionBuildersMaps.get(file.getFileSet()).add(file);
  }
  return Iterators.transform(partitionBuildersMaps.values().iterator(),
      new Function<FileSet.Builder<CopyEntity>, FileSet<CopyEntity>>() {
        @Nullable
        @Override
        public FileSet<CopyEntity> apply(@Nonnull FileSet.Builder<CopyEntity> input) {
          return input.build();
        }
      });
}
 
Example 5
Source File: NormalizedNodeContext.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
@Nullable Iterator<NormalizedNodeContext> iterateChildrenNamed(final DataContainerNode<?> data, final QName qname) {
    final NodeIdentifier arg = new NodeIdentifier(qname);
    final Optional<DataContainerChild<? extends @Nullable PathArgument, ?>> maybeChild = data.getChild(arg);
    if (!maybeChild.isPresent()) {
        return null;
    }

    final NormalizedNode<?, ?> child = maybeChild.get();
    final Collection<? extends NormalizedNode<?, ?>> collection;

    // The child may be a structural node
    if (child instanceof MapNode) {
        collection = ((MapNode)child).getValue();
    } else if (child instanceof LeafSetNode) {
        collection = ((LeafSetNode<?>)child).getValue();
    } else {
        return Iterators.singletonIterator(createChild(child));
    }

    return Iterators.transform(collection.iterator(), this::createChild);
}
 
Example 6
Source File: ReflectionClassDef.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
@Nonnull @Override public Set<String> getInterfaces() {
    return new AbstractSet<String>() {
        @Nonnull @Override public Iterator<String> iterator() {
            return Iterators.transform(Iterators.forArray(cls.getInterfaces()), new Function<Class, String>() {
                @Nullable @Override public String apply(@Nullable Class input) {
                    if (input == null) {
                        return null;
                    }
                    return ReflectionUtils.javaToDexName(input.getName());
                }
            });
        }

        @Override public int size() {
            return cls.getInterfaces().length;
        }
    };
}
 
Example 7
Source File: InternalAdapterStoreImpl.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Override
public String[] getTypeNames() {
  final MetadataReader reader = getReader(false);
  if (reader == null) {
    return new String[0];
  }
  final CloseableIterator<GeoWaveMetadata> results =
      reader.query(new MetadataQuery(null, INTERNAL_TO_EXTERNAL_ID));
  try (CloseableIterator<String> it =
      new CloseableIteratorWrapper<>(
          results,
          Iterators.transform(
              results,
              input -> StringUtils.stringFromBinary(input.getValue())))) {
    return Iterators.toArray(it, String.class);
  }
}
 
Example 8
Source File: StorageIterators.java    From exonum-java-binding with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new iterator over an index.
 *
 * <p>The returned iterator is a {@link ConfigurableRustIter}
 * wrapped in a {@link RustIterAdapter}.
 *
 * @param nativeHandle nativeHandle of this iterator
 * @param nextFunction a function to call to get the next item
 * @param disposeOperation an operation to call to destroy the corresponding native iterator
 * @param collectionAccess a database access of the collection over which to iterate
 * @param modificationCounter a modification counter of the collection
 * @param transformingFunction a function to apply to elements returned by native iterator
 *                             (usually, to an array of bytes)
 */
static <ElementT, NativeT> Iterator<ElementT> createIterator(
    long nativeHandle,
    LongFunction<NativeT> nextFunction,
    LongConsumer disposeOperation,
    AbstractAccess collectionAccess,
    ModificationCounter modificationCounter,
    Function<? super NativeT, ? extends ElementT> transformingFunction) {

  // Register the destructor first.
  NativeHandle handle = new NativeHandle(nativeHandle);
  Cleaner cleaner = collectionAccess.getCleaner();
  cleaner.add(new ProxyDestructor(handle, RustIter.class, disposeOperation));

  Iterator<NativeT> iterator = new RustIterAdapter<>(
      new ConfigurableRustIter<>(
          handle,
          nextFunction,
          modificationCounter
      )
  );

  return Iterators.transform(iterator, transformingFunction::apply);
}
 
Example 9
Source File: NoJdtTestLanguageGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
	Iterator<Greeting> filtered = Iterators.filter(resource.getAllContents(), Greeting.class);
	Iterator<String> names = Iterators.transform(filtered, new Function<Greeting, String>() {

		@Override
		public String apply(Greeting greeting) {
			return greeting.getName();
		}
	});
	String fileName = resource.getURI().lastSegment();
	if(fileName == null) fileName = "greetings";
	fsa.generateFile(fileName+".txt", "People to greet: " + IteratorExtensions.join(names, ", "));
}
 
Example 10
Source File: CompactionSource.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private List<Dataset> prioritize (List<Dataset> datasets, State state) {
  double maxPool = state.getPropAsDouble(MRCompactor.COMPACTION_DATASETS_MAX_COUNT, MRCompactor.DEFUALT_COMPACTION_DATASETS_MAX_COUNT);
  ResourcePool pool = ResourcePool.builder().maxResource(SimpleDatasetRequest.SIMPLE_DATASET_COUNT_DIMENSION, maxPool).build();

  Iterator<Dataset> newList = Iterators.transform(
      this.allocator.allocateRequests(datasets.stream().map(SimpleDatasetRequestor::new).iterator(), pool), (input) -> input.getDataset());
  return Lists.newArrayList(newList);
}
 
Example 11
Source File: AvroTupleWrapper.java    From spork with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Object> iterator() {
  return Iterators.transform(avroObject.getSchema().getFields().iterator(),
      new Function<Schema.Field, Object>() {
          @Override
          public Object apply(final Field f) {
            return avroObject.get(f.pos());
          }
        }
      );
}
 
Example 12
Source File: ReadWalkerSpark.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static FlatMapFunction<Iterator<GATKRead>, ReadWalkerContext> getReadsFunction(
        String referenceFileName, Broadcast<FeatureManager> bFeatureManager) {
    return readIterator -> {
        ReferenceDataSource reference = referenceFileName == null ? null : new ReferenceFileSource(IOUtils.getPath(SparkFiles.get(referenceFileName)));
        FeatureManager features = bFeatureManager == null ? null : bFeatureManager.getValue();
        return Iterators.transform(readIterator, new Function<GATKRead, ReadWalkerContext>() {
            @Nullable
            @Override
            public ReadWalkerContext apply(@Nullable GATKRead r) {
                final SimpleInterval readInterval = getReadInterval(r);
                return new ReadWalkerContext(r, new ReferenceContext(reference, readInterval), new FeatureContext(features, readInterval));
            }
        });
    };
}
 
Example 13
Source File: RangeStreamScannerTest.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * FOO == 'boohoo' hits day 20190319 with 15 shards, each shard has 25 document ids.
 */
@Test
public void testExceedShardsPerDayThresholdAndDocumentsPerShardThreshold() throws Exception {
    
    // Components that define the query: "FOO == 'boohoo'"
    String fieldName = "FOO";
    String fieldValue = "boohoo";
    ASTEQNode eqNode = (ASTEQNode) JexlNodeFactory.buildEQNode(fieldName, fieldValue);
    
    // Construct a ScannerStream from RangeStreamScanner, iterator, entry parser.
    RangeStreamScanner rangeStreamScanner = buildRangeStreamScanner(fieldName, fieldValue);
    EntryParser entryParser = new EntryParser(eqNode, fieldName, fieldValue, config.getIndexedFields());
    Iterator<Tuple2<String,IndexInfo>> iterator = Iterators.transform(rangeStreamScanner, entryParser);
    ScannerStream scannerStream = ScannerStream.initialized(iterator, eqNode);
    
    // Assert the iterator correctly iterates over the iterables without irritating the unit test.
    assertTrue(scannerStream.hasNext());
    int shardCount = 0;
    int documentCount = 0;
    while (scannerStream.hasNext()) {
        Tuple2<String,IndexInfo> entry = scannerStream.next();
        assertTrue("Expected shard to start with '20190323' but was: " + entry.first(), entry.first().startsWith("20190323"));
        shardCount++;
        documentCount += entry.second().count();
    }
    // A single range with a count of -1 means the shard ranges were collapsed into a day range.
    assertEquals(1, shardCount);
    assertEquals(-1, documentCount);
    assertFalse(scannerStream.hasNext());
}
 
Example 14
Source File: TxCacheDB.java    From gsc-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Iterator<Map.Entry<byte[], byte[]>> iterator() {
    return Iterators.transform(db.entrySet().iterator(),
            e -> Maps.immutableEntry(e.getKey().getBytes(), Longs.toByteArray(e.getValue())));
}
 
Example 15
Source File: XlsReaderImpl.java    From ganttproject with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Iterator<SpreadsheetRecord> iterator() {
  return Iterators.transform(myBook.getSheetAt(0).iterator(), (input) -> new XlsRecordImpl(getCellValues(input), myHeaders));
}
 
Example 16
Source File: Table.java    From kareldb with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator iterator() {
    return Iterators.transform(cache.all(), Table.this::toRow);
}
 
Example 17
Source File: ZipInputFileProvider.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<String> iterator() {
  return Iterators.transform(
      Iterators.forEnumeration(zipFile.entries()), Functions.toStringFunction());
}
 
Example 18
Source File: ZWaveController.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<DeviceInfo> iterator() {
   Iterator<ZWNode> allNodesIterator = zwNetwork.getNodes().iterator();
   Iterator<ZWNode> nodeIterator = Iterators.<ZWNode>filter(allNodesIterator, n -> n.getNodeId() != ZWConfig.GATEWAY_NODE_ID);
   return Iterators.<ZWNode, DeviceInfo> transform(nodeIterator, n -> new ZWDeviceInfo(n));
}
 
Example 19
Source File: InformationSchemaRecordReader.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private TableWriter<?> createTableWriter() {
  final Set<String> selectedFields =
    isStarQuery() ? InformationSchemaMetadata.getAllFieldNames(table.getRecordSchema()) : getGivenFields();

  switch (table) {

    case CATALOGS: {
      final ListCatalogsRequest.Builder catalogsRequest = ListCatalogsRequest.newBuilder()
        .setUsername(username);
      if (searchQuery != null) {
        catalogsRequest.setQuery(searchQuery);
      }

      // start Catalog stream from catalog service
      return new CatalogsTableWriter(catalogStub.listCatalogs(catalogsRequest.build()), selectedFields, catalogName);
    }

    case SCHEMATA: {
      final ListSchemataRequest.Builder schemataRequest = ListSchemataRequest.newBuilder()
        .setUsername(username);
      if (searchQuery != null) {
        schemataRequest.setQuery(searchQuery);
      }

      // start Schema stream from catalog service
      return new SchemataTableWriter(catalogStub.listSchemata(schemataRequest.build()), selectedFields, catalogName);
    }

    case TABLES: {
      final ListTablesRequest.Builder tablesRequest = ListTablesRequest.newBuilder()
        .setUsername(username);
      if (searchQuery != null) {
        tablesRequest.setQuery(searchQuery);
      }

      // start Table stream from catalog service
      return new TablesTableWriter(catalogStub.listTables(tablesRequest.build()), selectedFields, catalogName);
    }

    case VIEWS: {
      final ListViewsRequest.Builder viewsRequest = ListViewsRequest.newBuilder()
        .setUsername(username);
      if (searchQuery != null) {
        viewsRequest.setQuery(searchQuery);
      }

      // start View stream from catalog service
      return new ViewsTableWriter(catalogStub.listViews(viewsRequest.build()), selectedFields, catalogName);
    }

    case COLUMNS: {
      final ListTableSchemataRequest.Builder columnsRequest = ListTableSchemataRequest.newBuilder()
        .setUsername(username);
      if (searchQuery != null) {
        columnsRequest.setQuery(searchQuery);
      }

      // start TableSchema stream from catalog service
      final Iterator<TableSchema> tableSchemata = catalogStub.listTableSchemata(columnsRequest.build());

      // For each TableSchema, iterates over #flatMap of batch_schema field, which represents the records in the
      // "COLUMNS" table, and not the TableSchema message itself (unlike other tables).
      final Iterator<Column> columnIterator = new AbstractIterator<Column>() {
        Iterator<Column> currentIterator = null;

        @Override
        protected Column computeNext() {
          while (true) {
            if (currentIterator != null && currentIterator.hasNext()) {
              return currentIterator.next();
            }

            if (!tableSchemata.hasNext()) {
              return endOfData();
            }

            // Gets next TableSchema from the catalog service only after exhausting the current one. See comment in
            // TableWriter#write.
            final TableSchema currentSchema = tableSchemata.next();
            final RelDataType rowType =
              CalciteArrowHelper.wrap(BatchSchema.deserialize(currentSchema.getBatchSchema().toByteArray()))
                .toCalciteRecordType(JavaTypeFactoryImpl.INSTANCE);
            //noinspection ConstantConditions
            currentIterator = Iterators.transform(rowType.getFieldList().iterator(),
              field -> new Column(Strings.isNullOrEmpty(catalogName) ? currentSchema.getCatalogName() : catalogName,
                currentSchema.getSchemaName(),
                currentSchema.getTableName(),
                field));
          }
        }
      };
      return new ColumnsTableWriter(columnIterator, selectedFields, catalogName);
    }

    default:
      throw UserException.unsupportedError()
        .message("InformationSchemaRecordReader does not support table of '%s' type", table)
        .buildSilently();
  }
}
 
Example 20
Source File: CollectionBucket.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<Row> iterator() {
    return Iterators.transform(rows.iterator(), Buckets.arrayToRowFunction());
}