Example usage for java.util LinkedList addAll

List of usage examples for java.util LinkedList addAll

Introduction

In this page you can find the example usage for java.util LinkedList addAll.

Prototype

public boolean addAll(int index, Collection<? extends E> c) 

Source Link

Document

Inserts all of the elements in the specified collection into this list, starting at the specified position.

Usage

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // create a new collection and add some elements
    Collection collection = new ArrayList();
    collection.add("One");
    collection.add("Two");
    collection.add("Three");

    // add the collection in the LinkedList at index 2
    list.addAll(2, collection);

    // print the new list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static int deepHashCode(Object obj) {
    Set visited = new HashSet();
    LinkedList<Object> stack = new LinkedList<Object>();
    stack.addFirst(obj);/*  w  w  w  .j a  v a  2  s . c o m*/
    int hash = 0;

    while (!stack.isEmpty()) {
        obj = stack.removeFirst();
        if (obj == null || visited.contains(obj)) {
            continue;
        }

        visited.add(obj);

        if (obj.getClass().isArray()) {
            int len = Array.getLength(obj);
            for (int i = 0; i < len; i++) {
                stack.addFirst(Array.get(obj, i));
            }
            continue;
        }

        if (obj instanceof Collection) {
            stack.addAll(0, (Collection) obj);
            continue;
        }

        if (obj instanceof Map) {
            stack.addAll(0, ((Map) obj).keySet());
            stack.addAll(0, ((Map) obj).values());
            continue;
        }

        if (hasCustomHashCode(obj.getClass())) {
            hash += obj.hashCode();
            continue;
        }

        Collection<Field> fields = getDeepDeclaredFields(obj.getClass());
        for (Field field : fields) {
            try {
                stack.addFirst(field.get(obj));
            } catch (Exception ignored) {
            }
        }
    }
    return hash;
}

From source file:com.jnj.b2b.core.search.solrfacetsearch.provider.entity.VariantValueCategoryModelPriorityComparator.java

private LinkedList<CategoryModel> getPathToRoot(final VariantValueCategoryModel variantValueCategory) {
    final LinkedList<CategoryModel> pathToRoot = new LinkedList<>(
            getCategoryService().getPathForCategory(variantValueCategory));

    while (!getCategoryService().isRoot(pathToRoot.get(0))) {
        pathToRoot.addAll(0, getCategoryService().getPathForCategory(pathToRoot.get(0)));
    }/*from   w w w.j  a  v a  2  s .  c o  m*/

    return pathToRoot;
}

From source file:org.dcm4che3.conf.core.misc.DeepEquals.java

/**
 * Get a deterministic hashCode (int) value for an Object, regardless of
 * when it was created or where it was loaded into memory.  The problem
 * with java.lang.Object.hashCode() is that it essentially relies on
 * memory location of an object (what identity it was assigned), whereas
 * this method will produce the same hashCode for any object graph, regardless
 * of how many times it is created.<br/><br/>
 *
 * This method will handle cycles correctly (A->B->C->A).  In this case,
 * Starting with object A, B, or C would yield the same hashCode.  If an
 * object encountered (root, suboject, etc.) has a hashCode() method on it
 * (that is not Object.hashCode()), that hashCode() method will be called
 * and it will stop traversal on that branch.
 * @param obj Object who hashCode is desired.
 * @return the 'deep' hashCode value for the passed in object.
 *//*from   ww  w . j  a va  2s .  c  om*/
public static int deepHashCode(Object obj) {
    Set visited = new HashSet();
    LinkedList<Object> stack = new LinkedList<Object>();
    stack.addFirst(obj);
    int hash = 0;

    while (!stack.isEmpty()) {
        obj = stack.removeFirst();
        if (obj == null || visited.contains(obj)) {
            continue;
        }

        visited.add(obj);

        if (obj.getClass().isArray()) {
            int len = Array.getLength(obj);
            for (int i = 0; i < len; i++) {
                stack.addFirst(Array.get(obj, i));
            }
            continue;
        }

        if (obj instanceof Collection) {
            stack.addAll(0, (Collection) obj);
            continue;
        }

        if (obj instanceof Map) {
            stack.addAll(0, ((Map) obj).keySet());
            stack.addAll(0, ((Map) obj).values());
            continue;
        }

        if (hasCustomHashCode(obj.getClass())) { // A real hashCode() method exists, call it.
            hash += obj.hashCode();
            continue;
        }

        Collection<Field> fields = getDeepDeclaredFields(obj.getClass());
        for (Field field : fields) {
            try {
                stack.addFirst(field.get(obj));
            } catch (Exception ignored) {
            }
        }
    }
    return hash;
}

From source file:de.tarent.maven.plugins.pkg.Utils.java

/**
 * A <code>TargetConfiguration</code> can depend on another and so multiple
 * build processes might need to be run for a single
 * <code>TargetConfiguration</code>.
 * /*from  w w w .j a va 2s. c o  m*/
 * <p>
 * The list of <code>TargetConfiguration</code> instance that need to be
 * built is called a build chain. A build chain with <code>n</code> entries
 * contains <code>n-1</code> instances that need to be built before. The
 * last element in the build chain is the one that was initially requested
 * and must be build last.
 * </p>
 * 
 * @param target
 * @param distro
 * @return
 * @throws MojoExecutionException
 */
public static List<TargetConfiguration> createBuildChain(String target, String distro,
        List<TargetConfiguration> targetConfigurations) throws MojoExecutionException {
    LinkedList<TargetConfiguration> tcs = new LinkedList<TargetConfiguration>();

    // Merges vertically, that means through the 'parent' property.
    TargetConfiguration tc = Utils.getTargetConfigurationFromString(target, targetConfigurations);
    // Utils.getMergedConfigurationImpl(target, distro,
    // targetConfigurations, true);

    // In getMergedConfiguraion we check if targets that are hierarchically
    // related
    // support the same distro. Here we will have to check again, as there
    // may not
    // be parent-child relationship between them.

    if (tc.getDistros().contains(distro)) {
        tcs.addFirst(tc);

        List<String> relations = tc.getRelations();
        for (String relation : relations) {
            tcs.addAll(0, createBuildChain(relation, distro, targetConfigurations));
        }
        return tcs;
    } else {
        throw new MojoExecutionException(
                "target configuration '" + tc.getTarget() + "' does not support distro: " + distro);
    }
}

From source file:com.tulskiy.musique.library.Library.java

public void rescan(Map<String, Object> progress) {
    List<String> folders = LibraryConfiguration.getFolders();
    if (CollectionUtils.isEmpty(folders)) {
        return;/*from   w w  w. j  av a  2s  . c  o m*/
    }
    progress.put("processing.file", "");

    data.removeDeadItems();

    HashMap<TrackData, Track> trackDatas = new HashMap<TrackData, Track>();
    for (Track track : data) {
        trackDatas.put(track.getTrackData(), track);
    }

    LinkedList<File> queue = new LinkedList<File>();
    for (String path : folders) {
        File f = new File(path);
        if (f.exists())
            queue.add(f);
    }

    HashSet<Track> processed = new HashSet<Track>();
    final Set<String> formats = Codecs.getFormats();
    ArrayList<Track> temp = new ArrayList<Track>();
    while (!queue.isEmpty()) {
        try {
            File file = queue.pop();
            if (progress != null) {
                if (progress.get("processing.stop") != null) {
                    break;
                }
                progress.put("processing.file", file.getAbsolutePath());
            }
            if (file.isDirectory()) {
                queue.addAll(0, Arrays.asList(file.listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File file) {
                        if (file.isHidden() || !file.canRead()) {
                            return false;
                        }

                        if (file.isDirectory())
                            return true;

                        String ext = Util.getFileExt(file).toLowerCase();
                        if (formats.contains(ext)) {
                            String name = Util.removeExt(file.getAbsolutePath()) + ".cue";
                            return !new File(name).exists();
                        }
                        return ext.equals("cue");
                    }
                })));
            } else {
                TrackData trackData = new TrackData(file.toURI(), 0);
                Track track = trackDatas.get(trackData);
                if (track != null) {
                    if (track.getTrackData().getLastModified() != file.lastModified()) {
                        track.getTrackData().clearTags();
                        TrackIO.getAudioFileReader(file.getName()).reload(track);
                    }
                    processed.add(track);
                } else {
                    temp.clear();
                    TrackIO.getAudioFileReader(file.getName()).read(file, temp);
                    for (Track newTrack : temp) {
                        trackData = newTrack.getTrackData();
                        if (trackDatas.containsKey(trackData)) {
                            // it must be the cue file, so  merge the track data
                            trackData.merge(newTrack.getTrackData());
                        }
                        processed.add(newTrack);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    data.clear();
    data.addAll(processed);
    processed.clear();
    trackDatas.clear();
    rebuildTree();
}

From source file:net.stickycode.mockwire.spring30.MockwireFieldInjectionAnnotationBeanPostProcessor.java

private InjectionMetadata buildAutowiringMetadata(Class clazz) {
    LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    Class<?> targetClass = clazz;

    do {//from  w w  w.  j a  v  a2s.co m
        LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
        for (Field field : targetClass.getDeclaredFields()) {
            Annotation annotation = findAutowiredAnnotation(field);
            if (annotation != null) {
                if (Modifier.isStatic(field.getModifiers())) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Autowired annotation is not supported on static fields: " + field);
                    }
                    continue;
                }
                boolean required = determineRequiredStatus(annotation);
                currElements.add(new AutowiredFieldElement(field, required));
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
    } while (targetClass != null && targetClass != Object.class);

    return new InjectionMetadata(clazz, elements);
}

From source file:org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.java

private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
    final boolean debug = logger.isDebugEnabled();
    LinkedList<LifecycleElement> initMethods = new LinkedList<>();
    LinkedList<LifecycleElement> destroyMethods = new LinkedList<>();
    Class<?> targetClass = clazz;

    do {//from w w  w . ja  v a2 s  . c  om
        final LinkedList<LifecycleElement> currInitMethods = new LinkedList<>();
        final LinkedList<LifecycleElement> currDestroyMethods = new LinkedList<>();

        ReflectionUtils.doWithLocalMethods(targetClass, method -> {
            if (initAnnotationType != null) {
                if (method.getAnnotation(initAnnotationType) != null) {
                    LifecycleElement element = new LifecycleElement(method);
                    currInitMethods.add(element);
                    if (debug) {
                        logger.debug("Found init method on class [" + clazz.getName() + "]: " + method);
                    }
                }
            }
            if (destroyAnnotationType != null) {
                if (method.getAnnotation(destroyAnnotationType) != null) {
                    currDestroyMethods.add(new LifecycleElement(method));
                    if (debug) {
                        logger.debug("Found destroy method on class [" + clazz.getName() + "]: " + method);
                    }
                }
            }
        });

        initMethods.addAll(0, currInitMethods);
        destroyMethods.addAll(currDestroyMethods);
        targetClass = targetClass.getSuperclass();
    } while (targetClass != null && targetClass != Object.class);

    return new LifecycleMetadata(clazz, initMethods, destroyMethods);
}

From source file:de.taimos.dvalin.test.jaxrs.TestProxyBeanPostProcessor.java

private InjectionMetadata buildResourceMetadata(Class<?> clazz) {
    LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    Class<?> targetClass = clazz;

    do {/* www  . j  ava2s  .  co  m*/
        LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
        for (Field field : targetClass.getDeclaredFields()) {
            if (field.isAnnotationPresent(TestProxy.class)) {
                if (Modifier.isStatic(field.getModifiers())) {
                    throw new IllegalStateException("@TestProxy annotation is not supported on static fields");
                }
                currElements.add(new TestProxyElement(field, null));
            }
        }
        for (Method method : targetClass.getDeclaredMethods()) {
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                continue;
            }
            if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (bridgedMethod.isAnnotationPresent(TestProxy.class)) {
                    if (Modifier.isStatic(method.getModifiers())) {
                        throw new IllegalStateException(
                                "@TestProxy annotation is not supported on static methods");
                    }
                    if (method.getParameterTypes().length != 1) {
                        throw new IllegalStateException(
                                "@TestProxy annotation requires a single-arg method: " + method);
                    }
                    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                    currElements.add(new TestProxyElement(method, pd));
                }
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
    } while ((targetClass != null) && (targetClass != Object.class));

    return new InjectionMetadata(clazz, elements);
}

From source file:de.taimos.dvalin.interconnect.core.spring.InterconnectBeanPostProcessor.java

private InjectionMetadata buildResourceMetadata(Class<?> clazz) {
    LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    Class<?> targetClass = clazz;

    do {/*  www  . ja va  2s.co m*/
        LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
        for (Field field : targetClass.getDeclaredFields()) {
            if (field.isAnnotationPresent(Interconnect.class)) {
                if (Modifier.isStatic(field.getModifiers())) {
                    throw new IllegalStateException(
                            "@Interconnect annotation is not supported on static fields");
                }
                currElements.add(new InterconnectElement(field, null));
            }
        }
        for (Method method : targetClass.getDeclaredMethods()) {
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                continue;
            }
            if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (bridgedMethod.isAnnotationPresent(Interconnect.class)) {
                    if (Modifier.isStatic(method.getModifiers())) {
                        throw new IllegalStateException(
                                "@Interconnect annotation is not supported on static methods");
                    }
                    if (method.getParameterTypes().length != 1) {
                        throw new IllegalStateException(
                                "@Interconnect annotation requires a single-arg method: " + method);
                    }
                    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                    currElements.add(new InterconnectElement(method, pd));
                }
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
    } while ((targetClass != null) && (targetClass != Object.class));

    return new InjectionMetadata(clazz, elements);
}