List of usage examples for java.util Iterator Iterator
Iterator
From source file:com.aliyun.odps.local.common.WareHouse.java
public Iterator<Object[]> readResourceTable(String project, String resource, final char inputColumnSeperator) throws IOException, OdpsException { if (!existsResource(project, resource)) { DownloadUtils.downloadResource(WareHouse.getInstance().getOdps(), getOdps().getDefaultProject(), resource, Constants.DEFAULT_DOWNLOAD_RECORD, inputColumnSeperator); }/*w ww.jav a 2s. com*/ File tableResourceDir = getReourceFile(project, resource); if (!tableResourceDir.isDirectory()) { throw new OdpsException("Resource " + project + "." + resource + " is not a valid file Resource, because it is not a direcotry"); } // LOG.info("Reading resource table from " + // tableResourceDir.getAbsolutePath()); TableInfo tableInfo = getReferencedTable(project, resource); PartitionSpec partitionSpec = PartitionUtils.convert(tableInfo.getPartSpec()); final List<File> datafiles = getDataFiles(project, tableInfo.getTableName(), partitionSpec, inputColumnSeperator); final Column[] schema = SchemaUtils.readSchema(getTableDir(project, tableInfo.getTableName())).getCols(); return new Iterator<Object[]>() { CsvReader reader; Object[] current; boolean fetched; @Override public boolean hasNext() { if (fetched) { return current != null; } // Fetch new one try { fetch(); } catch (IOException e) { throw new RuntimeException(e); } return current != null; } private void fetch() throws IOException { // first time if (reader == null) { if (datafiles.isEmpty()) { current = null; fetched = true; return; } File f = datafiles.remove(0); reader = DownloadUtils.newCsvReader(f.getAbsolutePath(), inputColumnSeperator, encoding); reader.setSafetySwitch(false); current = read(); fetched = true; return; } current = read(); if (current == null && !datafiles.isEmpty()) { File f = datafiles.remove(0); reader = DownloadUtils.newCsvReader(f.getAbsolutePath(), inputColumnSeperator, encoding); reader.setSafetySwitch(false); current = read(); fetched = true; return; } fetched = true; } @Override public Object[] next() { if (!hasNext()) { throw new NoSuchElementException(); } fetched = false; return current; } @Override public void remove() { throw new UnsupportedOperationException(); } private Object[] read() throws IOException { Object[] result; if (!reader.readRecord()) { return null; } String[] vals = reader.getValues(); if (vals == null || vals.length == 0) { result = null; } else { result = new Object[vals.length]; for (int i = 0; i < vals.length; i++) { result[i] = LocalRunUtils.fromString(schema[i].getType(), vals[i], "\\N"); } } return result; } }; }
From source file:org.nuclos.common.collection.CollectionUtils.java
/** * Returns an enumeration as an iterable. * @param enumeration the enumeration//from w w w. ja va 2 s.c o m * @return an iterable wrapping the enumeration (note: remove is not supported) * @precondition enumeration != null * @postcondition result != null */ public static <T> Iterable<T> asIterable(final Enumeration<T> enumeration) { return new Iterable<T>() { @Override public Iterator<T> iterator() { return new Iterator<T>() { @Override public boolean hasNext() { return enumeration.hasMoreElements(); } @Override public T next() { return enumeration.nextElement(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; } }; }
From source file:org.apache.hadoop.yarn.server.resourcemanager.ClientRMService.java
/** * Get applications matching the {@link GetApplicationsRequest}. If * caseSensitive is set to false, applicationTypes in * GetApplicationRequest are expected to be in all-lowercase *///from ww w. java 2 s.com @Private public GetApplicationsResponse getApplications(GetApplicationsRequest request, boolean caseSensitive) throws YarnException { UserGroupInformation callerUGI; try { callerUGI = UserGroupInformation.getCurrentUser(); } catch (IOException ie) { LOG.info("Error getting UGI ", ie); throw RPCUtil.getRemoteException(ie); } Set<String> applicationTypes = request.getApplicationTypes(); EnumSet<YarnApplicationState> applicationStates = request.getApplicationStates(); Set<String> users = request.getUsers(); Set<String> queues = request.getQueues(); Set<String> tags = request.getApplicationTags(); long limit = request.getLimit(); LongRange start = request.getStartRange(); LongRange finish = request.getFinishRange(); ApplicationsRequestScope scope = request.getScope(); final Map<ApplicationId, RMApp> apps = rmContext.getRMApps(); Iterator<RMApp> appsIter; // If the query filters by queues, we can avoid considering apps outside // of those queues by asking the scheduler for the apps in those queues. if (queues != null && !queues.isEmpty()) { // Construct an iterator over apps in given queues // Collect list of lists to avoid copying all apps final List<List<ApplicationAttemptId>> queueAppLists = new ArrayList<List<ApplicationAttemptId>>(); for (String queue : queues) { List<ApplicationAttemptId> appsInQueue = scheduler.getAppsInQueue(queue); if (appsInQueue != null && !appsInQueue.isEmpty()) { queueAppLists.add(appsInQueue); } } appsIter = new Iterator<RMApp>() { Iterator<List<ApplicationAttemptId>> appListIter = queueAppLists.iterator(); Iterator<ApplicationAttemptId> schedAppsIter; @Override public boolean hasNext() { // Because queueAppLists has no empty lists, hasNext is whether the // current list hasNext or whether there are any remaining lists return (schedAppsIter != null && schedAppsIter.hasNext()) || appListIter.hasNext(); } @Override public RMApp next() { if (schedAppsIter == null || !schedAppsIter.hasNext()) { schedAppsIter = appListIter.next().iterator(); } return apps.get(schedAppsIter.next().getApplicationId()); } @Override public void remove() { throw new UnsupportedOperationException("Remove not supported"); } }; } else { appsIter = apps.values().iterator(); } List<ApplicationReport> reports = new ArrayList<ApplicationReport>(); while (appsIter.hasNext() && reports.size() < limit) { RMApp application = appsIter.next(); // Check if current application falls under the specified scope if (scope == ApplicationsRequestScope.OWN && !callerUGI.getUserName().equals(application.getUser())) { continue; } if (applicationTypes != null && !applicationTypes.isEmpty()) { String appTypeToMatch = caseSensitive ? application.getApplicationType() : StringUtils.toLowerCase(application.getApplicationType()); if (!applicationTypes.contains(appTypeToMatch)) { continue; } } if (applicationStates != null && !applicationStates.isEmpty()) { if (!applicationStates.contains(application.createApplicationState())) { continue; } } if (users != null && !users.isEmpty() && !users.contains(application.getUser())) { continue; } if (start != null && !start.containsLong(application.getStartTime())) { continue; } if (finish != null && !finish.containsLong(application.getFinishTime())) { continue; } if (tags != null && !tags.isEmpty()) { Set<String> appTags = application.getApplicationTags(); if (appTags == null || appTags.isEmpty()) { continue; } boolean match = false; for (String tag : tags) { if (appTags.contains(tag)) { match = true; break; } } if (!match) { continue; } } // checkAccess can grab the scheduler lock so call it last boolean allowAccess = checkAccess(callerUGI, application.getUser(), ApplicationAccessType.VIEW_APP, application); if (scope == ApplicationsRequestScope.VIEWABLE && !allowAccess) { continue; } reports.add(application.createAndGetApplicationReport(callerUGI.getUserName(), allowAccess)); } GetApplicationsResponse response = recordFactory.newRecordInstance(GetApplicationsResponse.class); response.setApplicationList(reports); return response; }
From source file:org.apache.hadoop.hbase.backup.impl.BackupSystemTable.java
/** * Register WAL files as eligible for deletion * @param backupRoot root directory path to backup * @throws IOException exception//from ww w . j a v a 2 s . c om */ public Iterator<WALItem> getWALFilesIterator(String backupRoot) throws IOException { LOG.trace("get WAL files from backup system table"); final Table table = connection.getTable(tableName); Scan scan = createScanForGetWALs(backupRoot); final ResultScanner scanner = table.getScanner(scan); final Iterator<Result> it = scanner.iterator(); return new Iterator<WALItem>() { @Override public boolean hasNext() { boolean next = it.hasNext(); if (!next) { // close all try { scanner.close(); table.close(); } catch (IOException e) { LOG.error("Close WAL Iterator", e); } } return next; } @Override public WALItem next() { Result next = it.next(); List<Cell> cells = next.listCells(); byte[] buf = cells.get(0).getValueArray(); int len = cells.get(0).getValueLength(); int offset = cells.get(0).getValueOffset(); String backupId = new String(buf, offset, len); buf = cells.get(1).getValueArray(); len = cells.get(1).getValueLength(); offset = cells.get(1).getValueOffset(); String walFile = new String(buf, offset, len); buf = cells.get(2).getValueArray(); len = cells.get(2).getValueLength(); offset = cells.get(2).getValueOffset(); String backupRoot = new String(buf, offset, len); return new WALItem(backupId, walFile, backupRoot); } @Override public void remove() { // not implemented throw new RuntimeException("remove is not supported"); } }; }
From source file:com.github.helenusdriver.driver.impl.TableInfoImpl.java
/** * {@inheritDoc}/* w ww . j a v a 2 s . co m*/ * * @author paouelle * * @see com.github.helenusdriver.driver.info.TableInfo#columns() */ @Override public Iterator<FieldInfo<T>> columns() { final Iterator<FieldInfoImpl<T>> iterator = columns.values().iterator(); return new Iterator<FieldInfo<T>>() { private FieldInfoImpl<T> current = null; @Override public boolean hasNext() { return iterator.hasNext(); } @Override public FieldInfo<T> next() { this.current = iterator.next(); return current; } @SuppressWarnings("synthetic-access") @Override public void remove() { iterator.remove(); final String cname = current.getColumnName(); // make sure the remove the field from all internal maps too if (table != null) { primaryKeyColumns.remove(cname); partitionKeyColumns.remove(cname); finalPrimaryKeyValues.remove(cname); clusteringKeyColumns.remove(cname); final FieldInfoImpl<T> old = typeKeyColumn.getValue(); if ((old != null) && old.getColumnName().equals(cname)) { typeKeyColumn.setValue(null); } indexColumns.remove(cname); } mandatoryAndPrimaryKeyColumns.remove(cname); nonPrimaryKeyColumns.remove(cname); fields.remove(Pair.of(current.getName(), current.getDeclaringClass())); this.current = null; // clear the current pointer since it was removed! } }; }
From source file:cn.iie.haiep.hbase.value.Bytes.java
/** * Iterate over keys within the passed inclusive range. *//* w ww . ja v a2 s .com*/ public static Iterable<byte[]> iterateOnSplits(final byte[] a, final byte[] b, final int num) { byte[] aPadded; byte[] bPadded; if (a.length < b.length) { aPadded = padTail(a, b.length - a.length); bPadded = b; } else if (b.length < a.length) { aPadded = a; bPadded = padTail(b, a.length - b.length); } else { aPadded = a; bPadded = b; } if (compareTo(aPadded, bPadded) >= 0) { throw new IllegalArgumentException("b <= a"); } if (num <= 0) { throw new IllegalArgumentException("num cannot be < 0"); } byte[] prependHeader = { 1, 0 }; final BigInteger startBI = new BigInteger(add(prependHeader, aPadded)); final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded)); final BigInteger diffBI = stopBI.subtract(startBI); final BigInteger splitsBI = BigInteger.valueOf(num + 1); if (diffBI.compareTo(splitsBI) < 0) { return null; } final BigInteger intervalBI; try { intervalBI = diffBI.divide(splitsBI); } catch (Exception e) { LOG.error("Exception caught during division", e); return null; } final Iterator<byte[]> iterator = new Iterator<byte[]>() { private int i = -1; @Override public boolean hasNext() { return i < num + 1; } @Override public byte[] next() { i++; if (i == 0) return a; if (i == num + 1) return b; BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i))); byte[] padded = curBI.toByteArray(); if (padded[1] == 0) padded = tail(padded, padded.length - 2); else padded = tail(padded, padded.length - 1); return padded; } @Override public void remove() { throw new UnsupportedOperationException(); } }; return new Iterable<byte[]>() { @Override public Iterator<byte[]> iterator() { return iterator; } }; }
From source file:objenome.util.ClassUtils.java
/** * Get an {@link Iterable} that can iterate over a class hierarchy in ascending (subclass to superclass) order. * * @param type the type to get the class hierarchy from * @param interfacesBehavior switch indicating whether to include or exclude interfaces * @return Iterable an Iterable over the class hierarchy of the given class * @since 3.2/* w w w .jav a2 s . c o m*/ */ public static Iterable<Class<?>> hierarchy(Class<?> type, Interfaces interfacesBehavior) { Iterable<Class<?>> classes = () -> { MutableObject<Class<?>> next = new MutableObject<>(type); return new Iterator<Class<?>>() { @Override public boolean hasNext() { return next.getValue() != null; } @Override public Class<?> next() { Class<?> result = next.getValue(); next.setValue(result.getSuperclass()); return result; } @Override public void remove() { throw new UnsupportedOperationException(); } }; }; if (interfacesBehavior != Interfaces.INCLUDE) { return classes; } return () -> { Set<Class<?>> seenInterfaces = new HashSet<>(); Iterator<Class<?>> wrapped = classes.iterator(); return new Iterator<Class<?>>() { Iterator<Class<?>> interfaces = Collections.<Class<?>>emptySet().iterator(); @Override public boolean hasNext() { return interfaces.hasNext() || wrapped.hasNext(); } @Override public Class<?> next() { if (interfaces.hasNext()) { Class<?> nextInterface = interfaces.next(); seenInterfaces.add(nextInterface); return nextInterface; } Class<?> nextSuperclass = wrapped.next(); Set<Class<?>> currentInterfaces = new LinkedHashSet<>(); walkInterfaces(currentInterfaces, nextSuperclass); interfaces = currentInterfaces.iterator(); return nextSuperclass; } private void walkInterfaces(Set<Class<?>> addTo, Class<?> c) { for (Class<?> iface : c.getInterfaces()) { if (!seenInterfaces.contains(iface)) { addTo.add(iface); } walkInterfaces(addTo, iface); } } @Override public void remove() { throw new UnsupportedOperationException(); } }; }; }
From source file:com.ery.ertc.estorm.util.Bytes.java
/** * Iterate over keys within the passed range. *///from w w w. ja v a 2 s .c o m public static Iterable<byte[]> iterateOnSplits(final byte[] a, final byte[] b, boolean inclusive, final int num) { byte[] aPadded; byte[] bPadded; if (a.length < b.length) { aPadded = padTail(a, b.length - a.length); bPadded = b; } else if (b.length < a.length) { aPadded = a; bPadded = padTail(b, a.length - b.length); } else { aPadded = a; bPadded = b; } if (compareTo(aPadded, bPadded) >= 0) { throw new IllegalArgumentException("b <= a"); } if (num <= 0) { throw new IllegalArgumentException("num cannot be < 0"); } byte[] prependHeader = { 1, 0 }; final BigInteger startBI = new BigInteger(add(prependHeader, aPadded)); final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded)); BigInteger diffBI = stopBI.subtract(startBI); if (inclusive) { diffBI = diffBI.add(BigInteger.ONE); } final BigInteger splitsBI = BigInteger.valueOf(num + 1); if (diffBI.compareTo(splitsBI) < 0) { return null; } final BigInteger intervalBI; try { intervalBI = diffBI.divide(splitsBI); } catch (Exception e) { LOG.error("Exception caught during division", e); return null; } final Iterator<byte[]> iterator = new Iterator<byte[]>() { private int i = -1; @Override public boolean hasNext() { return i < num + 1; } @Override public byte[] next() { i++; if (i == 0) return a; if (i == num + 1) return b; BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i))); byte[] padded = curBI.toByteArray(); if (padded[1] == 0) padded = tail(padded, padded.length - 2); else padded = tail(padded, padded.length - 1); return padded; } @Override public void remove() { throw new UnsupportedOperationException(); } }; return new Iterable<byte[]>() { @Override public Iterator<byte[]> iterator() { return iterator; } }; }
From source file:org.apache.hadoop.hbase.util.Bytes.java
/** * Iterate over keys within the passed range. *//*from w ww . jav a 2s.com*/ public static Iterable<byte[]> iterateOnSplits(final byte[] a, final byte[] b, boolean inclusive, final int num) { byte[] aPadded; byte[] bPadded; if (a.length < b.length) { aPadded = padTail(a, b.length - a.length); bPadded = b; } else if (b.length < a.length) { aPadded = a; bPadded = padTail(b, a.length - b.length); } else { aPadded = a; bPadded = b; } if (compareTo(aPadded, bPadded) >= 0) { throw new IllegalArgumentException("b <= a"); } if (num <= 0) { throw new IllegalArgumentException("num cannot be <= 0"); } byte[] prependHeader = { 1, 0 }; final BigInteger startBI = new BigInteger(add(prependHeader, aPadded)); final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded)); BigInteger diffBI = stopBI.subtract(startBI); if (inclusive) { diffBI = diffBI.add(BigInteger.ONE); } final BigInteger splitsBI = BigInteger.valueOf(num + 1); if (diffBI.compareTo(splitsBI) < 0) { return null; } final BigInteger intervalBI; try { intervalBI = diffBI.divide(splitsBI); } catch (Exception e) { LOG.error("Exception caught during division", e); return null; } final Iterator<byte[]> iterator = new Iterator<byte[]>() { private int i = -1; @Override public boolean hasNext() { return i < num + 1; } @Override public byte[] next() { i++; if (i == 0) return a; if (i == num + 1) return b; BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i))); byte[] padded = curBI.toByteArray(); if (padded[1] == 0) padded = tail(padded, padded.length - 2); else padded = tail(padded, padded.length - 1); return padded; } @Override public void remove() { throw new UnsupportedOperationException(); } }; return new Iterable<byte[]>() { @Override public Iterator<byte[]> iterator() { return iterator; } }; }
From source file:org.apache.kylin.common.util.Bytes.java
/** * Iterate over keys within the passed range. *//*from www.j av a 2s .c o m*/ public static Iterable<byte[]> iterateOnSplits(final byte[] a, final byte[] b, boolean inclusive, final int num) { byte[] aPadded; byte[] bPadded; if (a.length < b.length) { aPadded = padTail(a, b.length - a.length); bPadded = b; } else if (b.length < a.length) { aPadded = a; bPadded = padTail(b, a.length - b.length); } else { aPadded = a; bPadded = b; } if (compareTo(aPadded, bPadded) >= 0) { throw new IllegalArgumentException("b <= a"); } if (num <= 0) { throw new IllegalArgumentException("num cannot be <= 0"); } byte[] prependHeader = { 1, 0 }; final BigInteger startBI = new BigInteger(add(prependHeader, aPadded)); final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded)); BigInteger diffBI = stopBI.subtract(startBI); if (inclusive) { diffBI = diffBI.add(BigInteger.ONE); } final BigInteger splitsBI = BigInteger.valueOf(num + 1L); if (diffBI.compareTo(splitsBI) < 0) { return null; } final BigInteger intervalBI; try { intervalBI = diffBI.divide(splitsBI); } catch (Exception e) { LOG.error("Exception caught during division", e); return null; } final Iterator<byte[]> iterator = new Iterator<byte[]>() { private int i = -1; @Override public boolean hasNext() { return i < num + 1; } @Override public byte[] next() { i++; if (i == 0) return a; if (i == num + 1) return b; BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i))); byte[] padded = curBI.toByteArray(); if (padded[1] == 0) padded = tail(padded, padded.length - 2); else padded = tail(padded, padded.length - 1); return padded; } @Override public void remove() { throw new UnsupportedOperationException(); } }; return new Iterable<byte[]>() { @Override public Iterator<byte[]> iterator() { return iterator; } }; }