Search in sources :

Example 1 with AdHocIterator

use of com.cinchapi.common.base.AdHocIterator in project concourse by cinchapi.

the class EndpointContainer method endpoints.

/**
 * Return an {@link Iterable iterable} collection of all the
 * {@link Endpoint endpoints} that are defined in this container.
 *
 * @return all the defined endpoints
 */
public final Iterable<Endpoint> endpoints() {
    return new Iterable<Endpoint>() {

        private final Field[] fields = EndpointContainer.this.getClass().getDeclaredFields();

        private int i = 0;

        @Override
        public Iterator<Endpoint> iterator() {
            return new AdHocIterator<Endpoint>() {

                @Override
                protected Endpoint findNext() {
                    if (i >= fields.length) {
                        return null;
                    } else {
                        Field field = fields[i];
                        String name = field.getName();
                        ++i;
                        if (Endpoint.class.isAssignableFrom(field.getType())) {
                            Endpoint callable = Reflection.getCasted(field, EndpointContainer.this);
                            String action = callable.getAction();
                            String path = callable.getPath();
                            if (action == null && path == null && (name.startsWith("get") || name.startsWith("post") || name.startsWith("put") || name.startsWith("delete") || name.startsWith("upsert") || name.startsWith("options"))) {
                                List<String> args = AnyStrings.splitCamelCase(field.getName());
                                action = args.remove(0);
                                path = buildSparkPath(args);
                            }
                            path = AnyStrings.joinSimple(namespace, path);
                            Reflection.set("action", action, callable);
                            Reflection.set("path", path, callable);
                            return callable;
                        } else {
                            return findNext();
                        }
                    }
                }
            };
        }
    };
}
Also used : Field(java.lang.reflect.Field) AdHocIterator(com.cinchapi.common.base.AdHocIterator)

Example 2 with AdHocIterator

use of com.cinchapi.common.base.AdHocIterator in project concourse by cinchapi.

the class ObjectResultDataset method invert.

@Override
public Map<Object, Set<Long>> invert(String attribute) {
    return new TrackingMultimap<Object, Long>(Collections.emptyMap(), OBJECT_COMPARATOR) {

        @Override
        public boolean containsDataType(DataType type) {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).containsDataType(type);
        }

        @Override
        public long count() {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).count();
        }

        @Override
        public boolean delete(Object value, Long entity) {
            return thrift.delete(entity, attribute, Convert.javaToThrift(value));
        }

        @Override
        public double distinctiveness() {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).distinctiveness();
        }

        @Override
        public Set<Entry<Object, Set<Long>>> entrySet() {
            return new AbstractSet<Entry<Object, Set<Long>>>() {

                @Override
                public Iterator<Entry<Object, Set<Long>>> iterator() {
                    final Iterator<Entry<TObject, Set<Long>>> it = thrift.invertNullSafe(attribute).entrySet().iterator();
                    return new AdHocIterator<Entry<Object, Set<Long>>>() {

                        @Override
                        protected Entry<Object, Set<Long>> findNext() {
                            if (it.hasNext()) {
                                Entry<TObject, Set<Long>> entry = it.next();
                                return new SimpleEntry<>(Convert.thriftToJava(entry.getKey()), entry.getValue());
                            } else {
                                return null;
                            }
                        }
                    };
                }

                @Override
                public int size() {
                    return thrift.invertNullSafe(attribute).size();
                }
            };
        }

        @Override
        public boolean equals(Object obj) {
            if (this.getClass() == obj.getClass()) {
                Object entrySet = Reflection.call(obj, "entrySet");
                return entrySet().equals(entrySet);
            } else {
                return false;
            }
        }

        @Override
        public Set<Long> get(Object value) {
            return thrift.invertNullSafe(attribute).get(Convert.javaToThrift(value));
        }

        @Override
        public int hashCode() {
            return thrift.invertNullSafe(attribute).hashCode();
        }

        @Override
        public boolean hasValue(Long value) {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).hasValue(value);
        }

        @Override
        public boolean insert(Object value, Long entity) {
            return thrift.insert(entity, attribute, Convert.javaToThrift(value));
        }

        @Override
        public Set<Long> merge(Object value, Set<Long> entities) {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).merge(Convert.javaToThrift(value), entities);
        }

        @Override
        public double percentKeyDataType(DataType type) {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).percentKeyDataType(type);
        }

        @Override
        public double proportion(Object value) {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).proportion(Convert.javaToThrift(value));
        }

        @Override
        public Set<Long> put(Object value, Set<Long> entities) {
            Set<Long> stored = get(value);
            TObject tvalue = Convert.javaToThrift(value);
            entities.forEach(entity -> thrift.insert(entity, attribute, tvalue));
            return stored;
        }

        @Override
        public Set<Long> remove(Object value) {
            return thrift.invertNullSafe(attribute).remove(Convert.javaToThrift(value));
        }

        @Override
        public double spread() {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).spread();
        }

        @Override
        public String toString() {
            return thrift.invertNullSafe(attribute).toString();
        }

        @Override
        public double uniqueness() {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).uniqueness();
        }

        @Override
        public VariableType variableType() {
            return ((TrackingMultimap<TObject, Long>) thrift.invertNullSafe(attribute)).variableType();
        }

        @Override
        protected Set<Long> createValueSet() {
            // to the underyling #thrift based collection
            return null;
        }
    };
}
Also used : TObject(com.cinchapi.concourse.thrift.TObject) AbstractSet(java.util.AbstractSet) Set(java.util.Set) AdHocIterator(com.cinchapi.common.base.AdHocIterator) AbstractSet(java.util.AbstractSet) TObject(com.cinchapi.concourse.thrift.TObject)

Example 3 with AdHocIterator

use of com.cinchapi.common.base.AdHocIterator in project accent4j by cinchapi.

the class Application method classpath.

/**
 * Return a collection of {@link Path} objects that describe all the
 * elements that are on the current application's classpath.
 *
 * @return the current applications classpath
 */
public static Collection<Path> classpath() {
    ClassLoader loader = Application.class.getClassLoader();
    if (loader == null) {
        loader = ClassLoader.getSystemClassLoader();
    }
    URL[] urls = ((URLClassLoader) loader).getURLs();
    return new AbstractCollection<Path>() {

        @Override
        public Iterator<Path> iterator() {
            return new AdHocIterator<Path>() {

                private int index = -1;

                @Override
                protected Path findNext() {
                    ++index;
                    if (index < urls.length) {
                        URL url = urls[index];
                        try {
                            Path path = new File(url.toURI()).toPath();
                            if (Files.exists(path)) {
                                // isn't surprised
                                return path;
                            } else {
                                return findNext();
                            }
                        } catch (URISyntaxException e) {
                            throw CheckedExceptions.throwAsRuntimeException(e);
                        }
                    } else {
                        return null;
                    }
                }
            };
        }

        @Override
        public int size() {
            return urls.length;
        }
    };
}
Also used : Path(java.nio.file.Path) URLClassLoader(java.net.URLClassLoader) AdHocIterator(com.cinchapi.common.base.AdHocIterator) AbstractCollection(java.util.AbstractCollection) URLClassLoader(java.net.URLClassLoader) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Aggregations

AdHocIterator (com.cinchapi.common.base.AdHocIterator)3 TObject (com.cinchapi.concourse.thrift.TObject)1 File (java.io.File)1 Field (java.lang.reflect.Field)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Path (java.nio.file.Path)1 AbstractCollection (java.util.AbstractCollection)1 AbstractSet (java.util.AbstractSet)1 Set (java.util.Set)1