All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.netbeans.modules.java.classpath.DefaultGlobalPathRegistryImplementation Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.netbeans.modules.java.classpath;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.spi.java.classpath.GlobalPathRegistryImplementation;
import org.openide.util.lookup.ServiceProvider;

/**
 *
 * @author Tomas Zezula
 */
//@NotThreadSafe
@ServiceProvider(service = GlobalPathRegistryImplementation.class, position = 10_000)
public final class DefaultGlobalPathRegistryImplementation extends GlobalPathRegistryImplementation {

    private final Map> paths = new HashMap<>();

    @Override
    @NonNull
    protected Set getPaths(@NonNull final String id) {
        List l = paths.get(id);
        if (l != null && !l.isEmpty()) {
            return Collections.unmodifiableSet(new HashSet(l));
        } else {
            return Collections.emptySet();
        }
    }

    @Override
    @NonNull
    protected Set register(
            @NonNull final String id,
            @NonNull final ClassPath[] paths) {
        final Set added = new HashSet<>();
        List l = this.paths.get(id);
        if (l == null) {
            l = new ArrayList<>();
            this.paths.put(id, l);
        }
        for (ClassPath path : paths) {
            if (path == null) {
                throw new NullPointerException("Null path encountered in " + Arrays.asList(paths) + " of type " + id); // NOI18N
            }
            if (!added.contains(path) && !l.contains(path)) {
                added.add(path);
            }
            l.add(path);
        }
        return added;
    }

    @Override
    @NonNull
    protected Set unregister(
            @NonNull final String id,
            @NonNull final ClassPath[] paths) throws IllegalArgumentException {
        final Set removed = new HashSet<>();
        List l = this.paths.get(id);
        if (l == null) {
            l = new ArrayList<>();
        }
        List l2 = new ArrayList<>(l); // in case IAE thrown below
        for (ClassPath path : paths) {
            if (path == null) {
                throw new NullPointerException();
            }
            if (!l2.remove(path)) {
                throw new IllegalArgumentException("Attempt to remove nonexistent path [" + path +
                        "] from list of registered paths ["+l2+"] for id "+id+". All paths: "+this.paths); // NOI18N
            }
            if (!removed.contains(path) && !l2.contains(path)) {
                removed.add(path);
            }
        }
        this.paths.put(id, l2);
        return removed;
    }

    @Override
    @NonNull
    protected Set clear() {
        final Set removed = new HashSet<>();
        for (Iterator>> it = paths.entrySet().iterator();
            it.hasNext();) {
            final Map.Entry> e = it.next();
            removed.addAll(e.getValue());
            it.remove();
        }
        assert paths.isEmpty();
        return removed;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy