Example usage for java.util LinkedList isEmpty

List of usage examples for java.util LinkedList isEmpty

Introduction

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

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this list contains no elements.

Usage

From source file:elh.eus.absa.Features.java

/**
 *  Extract n-grams up to a certain length from an Conll tabulated format corpus.
 * /*from w w  w . j a va  2 s  .c o  m*/
 * @param int length : which 'n' use for 'n-grams' 
 * @param string type (wf|lemma|pos): what type of ngrams we want to extract.
 * @param boolean save : safe ngrams to file or not. 
 * @return TreeSet<String> return word form ngrams of length length
 */
private int extractNgramsTAB(int length, String type, List<String> discardPos, boolean save) {
    //System.err.println("ngram extraction Tab: _"+length+"_"+type);
    if (length == 0) {
        return 0;
    }

    for (String sent : corpus.getSentences().keySet()) {
        //System.err.println("ngram extraction, corpus sentences: "+corpus.getSentences().get(sent));           
        String[] tokens = corpus.getSentences().get(sent).split("\n");
        LinkedList<String> ngrams = new LinkedList<String>();
        for (String row : tokens) {
            String ngram = "";
            String[] fields = row.split("\t");
            String pos = "";
            switch (type) {
            case "wf":
                ngram = fields[0];
                break;
            case "lemma":
                if (fields.length > 1) {
                    ngram = fields[1];
                }
                if (fields.length > 2) {
                    pos = fields[2];
                }
                break;
            case "pos":
                if (fields.length > 2) {
                    ngram = fields[2];
                    switch (ngram.length()) {
                    case 0:
                        ngram = "-";
                        break;
                    case 1:
                        ngram = ngram.substring(0, 1);
                        break;
                    default:
                        ngram = ngram.substring(0, 2);
                        break;
                    }
                }
            }

            //if the is a blank line we assume sentence has ended and we empty and re-initialize the n-gram list 
            if (ngram.equals("")) {
                //empty n-gram list and add remaining n-grams to the feature list
                while (!ngrams.isEmpty()) {
                    String ng = featureFromArray(ngrams, type);
                    addNgram(type, ng);
                    ngrams.removeFirst();
                }
                continue;
            }

            if (ngrams.size() >= length) {
                ngrams.removeFirst();
            }

            //if no alphanumeric char is present discard the element as invalid ngram. Or if it has a PoS tag that
            //should be discarded
            String lCurrent = ngram;
            if ((!discardPos.contains(pos)) && (!ngram.matches("^[^\\p{L}\\p{M}\\p{Nd}\\p{InEmoticons}]+$"))
                    && (lCurrent.length() > 1)) {
                //standarize numeric values to NUMNUM lemma value
                //ngram.replaceFirst("^[0-9]$", "NUMNUM");
                if (!type.equalsIgnoreCase("pos")) {
                    ngrams.add(normalize(ngram, params.getProperty("normalization", "none")));
                } else {
                    ngrams.add(ngram);
                }
            }
            //certain punctuation marks are allowed as lemmas
            else if ((lCurrent.length() < 2) && (lCurrent.matches("[,;.?!]"))) {
                ngrams.add(lCurrent);
            }

            // add ngrams to the feature list
            for (int i = 0; i < ngrams.size(); i++) {
                String ng = featureFromArray(ngrams.subList(0, i + 1), type);
                addNgram(type, ng);
            }
        }
        //empty ngram list and add remaining ngrams to the feature list
        while (!ngrams.isEmpty()) {
            String ng = featureFromArray(ngrams, type);
            addNgram(type, ng);
            ngrams.removeFirst();
        }
    }
    return 1;
}

From source file:elh.eus.absa.Features.java

/**
 *  Extract n-grams up to a certain length from an Conll tabulated format string.
 * /*from   w w w  .ja  v  a2  s.  c om*/
 * @param String input : input tagged conll string 
 * @param int length : which 'n' use for 'n-grams' 
 * @param string type (wf|lemma|pos): what type of ngrams we want to extract.
 * @param boolean save : safe ngrams to file or not. 
 * @return int success: return 1 if the process ended correctly
 */
private int extractNgramsTABString(InputStream input, int length, String type, List<String> discardPos,
        boolean save) {
    //System.err.println("ngram extraction Tab: _"+length+"_"+type);
    if (length == 0) {
        return 0;
    }

    //System.err.println("ngram extraction, corpus sentences: "+corpus.getSentences().get(sent));                 
    //String[] tokens = input.split("\n");
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    LinkedList<String> ngrams = new LinkedList<String>();
    String line;
    try {
        while ((line = reader.readLine()) != null) {
            String ngram = "";
            String[] fields = line.split("\\s");
            String pos = "";
            switch (type) {
            case "wf":
                ngram = fields[0];
                break;
            case "lemma":
                if (fields.length > 1) {
                    ngram = fields[1];
                }
                if (fields.length > 2) {
                    pos = fields[2];
                }
                break;
            case "pos":
                if (fields.length > 2) {
                    ngram = fields[2];
                    switch (ngram.length()) {
                    case 0:
                        ngram = "-";
                        break;
                    case 1:
                        ngram = ngram.substring(0, 1);
                        break;
                    default:
                        ngram = ngram.substring(0, 2);
                        break;
                    }
                }
            }

            //if the is a blank line we assume sentence has ended and we empty and re-initialize the n-gram list 
            if (ngram.equals("")) {
                //empty n-gram list and add remaining n-grams to the feature list
                while (!ngrams.isEmpty()) {
                    String ng = featureFromArray(ngrams, type);
                    addNgram(type, ng);
                    ngrams.removeFirst();
                }
                continue;
            }

            if (ngrams.size() >= length) {
                ngrams.removeFirst();
            }

            //if no alphanumeric char is present discard the element as invalid ngram. Or if it has a PoS tag that
            //should be discarded
            String lCurrent = ngram;
            if ((!discardPos.contains(pos)) && (!ngram.matches("^[^\\p{L}\\p{M}\\p{Nd}\\p{InEmoticons}]+$"))
                    && (lCurrent.length() > 1)) {
                //standarize numeric values to NUMNUM lemma value
                //ngram.replaceFirst("^[0-9]$", "NUMNUM");
                if (!type.equalsIgnoreCase("pos")) {
                    ngrams.add(normalize(ngram, params.getProperty("normalization", "none")));
                } else {
                    ngrams.add(ngram);
                }
            }
            //certain punctuation marks are allowed as lemmas
            else if ((lCurrent.length() < 2) && (lCurrent.matches("[,;.?!]"))) {
                ngrams.add(lCurrent);
            }

            // add ngrams to the feature list
            for (int i = 0; i < ngrams.size(); i++) {
                String ng = featureFromArray(ngrams.subList(0, i + 1), type);
                addNgram(type, ng);
            }
        }
    } catch (IOException e) {
        System.err.println("EliXa::Features::extractNgramsTABString - WARNING: Error reading tagged file, "
                + "ngram extraction may be only partial\n");
    }

    //empty ngram list and add remaining ngrams to the feature list
    while (!ngrams.isEmpty()) {
        String ng = featureFromArray(ngrams, type);
        addNgram(type, ng);
        ngrams.removeFirst();
    }

    return 1;
}

From source file:org.diorite.config.impl.ConfigTemplateImpl.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private void scanMethods(LinkedList<MethodInvoker> methods, Map<String, MethodInvoker> toKeyMappers,
        Map<String, MethodInvoker> toStringMappers, Set<String> knownProperties) {
    int sizeBefore = methods.size();
    for (Iterator<MethodInvoker> iterator = methods.iterator(); iterator.hasNext();) {
        MethodInvoker methodInvoker = iterator.next();
        ConfigPropertyTemplateImpl template;
        Class<?> returnType = methodInvoker.getReturnType();
        Type genericReturnType = methodInvoker.getGenericReturnType();
        String name;//from   w  w w .  ja va 2  s.co  m
        Method method = methodInvoker.getMethod();
        if (methodInvoker.isPrivate()) {
            if (methodInvoker.isAnnotationPresent(ToKeyMapperFunction.class)) {
                this.scanKeyMapper(methodInvoker, toKeyMappers);
                iterator.remove();
                continue;
            }
            if (methodInvoker.isAnnotationPresent(ToStringMapperFunction.class)) {
                this.scanStringMapper(methodInvoker, toStringMappers);
                iterator.remove();
                continue;
            }
            name = this.extractName(methodInvoker);

            methodInvoker.ensureAccessible();
            if (!knownProperties.add(name)) {
                throw new IllegalStateException("Duplicated property: " + name);
            }
            template = new ConfigPropertyTemplateImpl(this, returnType, genericReturnType, name,
                    cfg -> methodInvoker.invoke(cfg), method);
        } else {
            Pair<ConfigPropertyAction, ActionMatcherResult> resultPair = ActionsRegistry.findMethod(method,
                    knownProperties::contains);
            if (resultPair == null) {
                iterator.remove();
                continue;
            }
            ConfigPropertyAction propertyAction = resultPair.getLeft();
            if (!propertyAction.getActionName().equals("get") && methodInvoker.isDefault()) {
                throw new RuntimeException("Unexpected default implementation of: " + method);
            }
            ActionMatcherResult matcherResult = resultPair.getRight();
            if (!matcherResult.isValidatedName()) {
                continue; // wait for validation.
            }
            name = matcherResult.getPropertyName();

            ConfigPropertyTemplateImpl<?> oldTemplate = this.mutableProperties.get(name);
            if (oldTemplate != null) {
                template = oldTemplate;
            } else {
                if (!propertyAction.declaresProperty()) {
                    continue;
                }
                if (!knownProperties.add(name)) {
                    throw new IllegalStateException("Duplicated property: " + name);
                }
                template = new ConfigPropertyTemplateImpl(this, returnType, genericReturnType, name,
                        cfg -> null, method);
            }

            if (propertyAction.getActionName().equals("get") && methodInvoker.isDefault()) {
                this.defaultValueFromDefaultMethod(methodInvoker, template);
            }

            MethodSignature methodSignature = new MethodSignature(method);
            PropertyActionKey propertyActionKey = new PropertyActionKey(propertyAction, methodSignature);
            this.mutableActions.put(propertyActionKey, template);
            this.actionsDispatcher.put(methodSignature, propertyActionKey);
        }
        this.order.add(name);
        this.mutableProperties.put(name, template);
        iterator.remove();
    }
    if (methods.isEmpty()) {
        return;
    }
    if (sizeBefore == methods.size()) {
        throw new IllegalStateException(
                "Can't create config template, can't find how to implement: " + methods);
    }
    this.scanMethods(methods, toKeyMappers, toStringMappers, knownProperties);
}

From source file:controllers.controller.java

private void insertarObjetoArchivo(HttpSession session, HttpServletRequest request,
        HttpServletResponse response, QUID quid, PrintWriter out, HashMap parameters, LinkedList filesToUpload,
        String FormFrom) throws Exception {
    if (parameters.get("idTipoArchivo") == null || parameters.get("idTipoArchivo").equals("")) {
        this.getServletConfig().getServletContext()
                .getRequestDispatcher("" + PageParameters.getParameter("msgUtil")
                        + "/msgNRedirect.jsp?title=Error&type=error&msg=Seleccione el tipo de archivo.&url="
                        + PageParameters.getParameter("mainContext") + PageParameters.getParameter("gui")
                        + "/Insert_ObjetoArchivo.jsp?" + WebUtil.encode(session, "imix") + "="
                        + WebUtil.encode(session, UTime.getTimeMilis()) + "_param_nombreObjeto="
                        + parameters.get("nombreObjeto") + "_param_idObjeto=" + parameters.get("idObjeto"))
                .forward(request, response);
    } else if (parameters.get("nombreArchivo") == null || parameters.get("nombreArchivo").equals("")) {
        this.getServletConfig().getServletContext()
                .getRequestDispatcher("" + PageParameters.getParameter("msgUtil")
                        + "/msgNRedirect.jsp?title=Error&type=error&msg=Escriba el nombre del archivo.&url="
                        + PageParameters.getParameter("mainContext") + PageParameters.getParameter("gui")
                        + "/Insert_ObjetoArchivo.jsp?" + WebUtil.encode(session, "imix") + "="
                        + WebUtil.encode(session, UTime.getTimeMilis()) + "_param_nombreObjeto="
                        + parameters.get("nombreObjeto") + "_param_idObjeto=" + parameters.get("idObjeto"))
                .forward(request, response);
    } else if (parameters.get("descripcion") == null
            || parameters.get("descripcion").toString().trim().equals("")) {
        this.getServletConfig().getServletContext()
                .getRequestDispatcher("" + PageParameters.getParameter("msgUtil")
                        + "/msgNRedirect.jsp?title=Error&type=error&msg=Escriba una descripcin.&url="
                        + PageParameters.getParameter("mainContext") + PageParameters.getParameter("gui")
                        + "/Insert_ObjetoArchivo.jsp?" + WebUtil.encode(session, "imix") + "="
                        + WebUtil.encode(session, UTime.getTimeMilis()) + "_param_nombreObjeto="
                        + parameters.get("nombreObjeto") + "_param_idObjeto=" + parameters.get("idObjeto"))
                .forward(request, response);
    } else if (parameters.get("tipoAcceso").equals("")) {
        this.getServletConfig().getServletContext().getRequestDispatcher(""
                + PageParameters.getParameter("msgUtil")
                + "/msgNRedirect.jsp?title=Error&type=error&msg=Seleccione el tipo de acceso para el archivo.&url="
                + PageParameters.getParameter("mainContext") + PageParameters.getParameter("gui")
                + "/Insert_ObjetoArchivo.jsp?" + WebUtil.encode(session, "imix") + "="
                + WebUtil.encode(session, UTime.getTimeMilis()) + "_param_nombreObjeto="
                + parameters.get("nombreObjeto") + "_param_idObjeto=" + parameters.get("idObjeto"))
                .forward(request, response);
    } else if (filesToUpload.isEmpty()) {
        this.getServletConfig().getServletContext().getRequestDispatcher(""
                + PageParameters.getParameter("msgUtil")
                + "/msgNRedirect.jsp?title=Error&type=error&msg=No ha seleccionado ningn archivo.&url="
                + PageParameters.getParameter("mainContext") + PageParameters.getParameter("gui")
                + "/Insert_ObjetoArchivo.jsp?" + WebUtil.encode(session, "imix") + "="
                + WebUtil.encode(session, UTime.getTimeMilis()) + "_param_nombreObjeto="
                + parameters.get("nombreObjeto") + "_param_idObjeto=" + parameters.get("idObjeto"))
                .forward(request, response);
    } else if (!filesToUpload.isEmpty()) {
        String idObjeto = WebUtil.decode(session, parameters.get("idObjeto").toString());
        String fechaActualizacion = UTime.calendar2SQLDateFormat(Calendar.getInstance());
        String descripcion = WebUtil.decode(session, parameters.get("descripcion").toString());
        String ubicacionFisica = PageParameters.getParameter("folderDocs");
        String idTipoArchivo = WebUtil.decode(session, parameters.get("idTipoArchivo").toString());
        String nombreObjeto = WebUtil.decode(session, parameters.get("nombreObjeto").toString());
        String keyWords = parameters.get("keywords").toString();
        String nombreArchivo = parameters.get("nombreArchivo").toString();
        String FK_ID_Plantel = session.getAttribute("FK_ID_Plantel").toString();

        //File verifyFolder = new File(PageParameters.getParameter("folderDocs"));
        File verifyFolder = new File(ubicacionFisica);
        if (!verifyFolder.exists()) {
            verifyFolder.mkdirs();/*from w  w w. ja v  a  2s  . c  o  m*/
        }
        int sucess = 0;
        for (int i = 0; i < filesToUpload.size(); i++) {
            FileItem itemToUpload = null;
            itemToUpload = (FileItem) filesToUpload.get(i);

            String extension = FileUtil.getExtension(itemToUpload.getName());
            String hashName = JHash.getFileDigest(itemToUpload.get(), "MD5") + extension;

            long tamanio = itemToUpload.getSize();

            if (this.validarDocumentExtension(session, request, response, quid, out, extension)) {
                File fileToWrite = new File(ubicacionFisica, hashName);
                Transporter tport = quid.insertArchivo4Objeto(idObjeto, nombreObjeto, idTipoArchivo,
                        nombreArchivo, descripcion, ubicacionFisica, extension, fechaActualizacion, tamanio,
                        WebUtil.decode(session, parameters.get("tipoAcceso").toString()).toLowerCase(),
                        keyWords, hashName, FK_ID_Plantel);
                if (tport.getCode() == 0) {
                    if (!fileToWrite.exists()) {
                        itemToUpload.write(fileToWrite);
                    }
                    sucess += 1;
                }
            } else {
                sucess = -1;
            }
        }
        if (sucess != -1) {
            this.getServletConfig().getServletContext()
                    .getRequestDispatcher("" + PageParameters.getParameter("msgUtil")
                            + "/msgNRedirect.jsp?title=Operacin Exitosa&type=info&msg=Se han guardado "
                            + sucess + " de " + filesToUpload.size() + " archivos.&url="
                            + PageParameters.getParameter("mainContext") + PageParameters.getParameter("gui")
                            + "/Insert_ObjetoArchivo.jsp?" + WebUtil.encode(session, "imix") + "="
                            + WebUtil.encode(session, UTime.getTimeMilis()) + "_param_nombreObjeto="
                            + parameters.get("nombreObjeto") + "_param_idObjeto=" + parameters.get("idObjeto"))
                    .forward(request, response);
        }

    }

}

From source file:org.sakaiproject.content.tool.FilePickerAction.java

/**
 * @param state/*from   www.ja va  2 s .c  om*/
 * @param homeCollectionId
 * @param currentCollectionId
 * @return
 */
public static List getCollectionPath(SessionState state) {
    logger.debug("ResourcesAction.getCollectionPath()");
    org.sakaiproject.content.api.ContentHostingService contentService = (org.sakaiproject.content.api.ContentHostingService) state
            .getAttribute(STATE_CONTENT_SERVICE);
    // make sure the channedId is set
    String currentCollectionId = (String) state.getAttribute(STATE_COLLECTION_ID);
    String homeCollectionId = (String) state.getAttribute(STATE_HOME_COLLECTION_ID);
    String navRoot = (String) state.getAttribute(STATE_NAVIGATION_ROOT);

    LinkedList collectionPath = new LinkedList();

    String previousCollectionId = "";
    List pathitems = new ArrayList();
    while ((currentCollectionId != null) && (!currentCollectionId.equals(navRoot))
            && (!currentCollectionId.equals(previousCollectionId))
            && !(contentService.ROOT_COLLECTIONS.contains(currentCollectionId))
            && (!contentService.isRootCollection(previousCollectionId))) {
        pathitems.add(currentCollectionId);
        previousCollectionId = currentCollectionId;
        currentCollectionId = contentService.getContainingCollectionId(currentCollectionId);
    }

    if (navRoot != null && (pathitems.isEmpty()
            || (!navRoot.equals(previousCollectionId) && !navRoot.equals(currentCollectionId)))) {
        pathitems.add(navRoot);

    }
    if (homeCollectionId != null && (pathitems.isEmpty()
            || (!homeCollectionId.equals(navRoot) && !homeCollectionId.equals(previousCollectionId)
                    && !homeCollectionId.equals(currentCollectionId)))) {
        pathitems.add(homeCollectionId);
    }

    Iterator items = pathitems.iterator();
    while (items.hasNext()) {
        String id = (String) items.next();
        try {
            ResourceProperties props = contentService.getProperties(id);
            String name = props.getPropertyFormatted(ResourceProperties.PROP_DISPLAY_NAME);
            String containingCollectionId = contentService.getContainingCollectionId(id);
            if (contentService.COLLECTION_DROPBOX.equals(containingCollectionId)) {
                Reference ref = EntityManager.newReference(contentService.getReference(id));
                Site site = SiteService.getSite(ref.getContext());
                String[] args = { site.getTitle() };
                name = trb.getFormattedMessage("title.dropbox", args);
            } else if (contentService.COLLECTION_SITE.equals(containingCollectionId)) {
                Reference ref = EntityManager.newReference(contentService.getReference(id));
                Site site = SiteService.getSite(ref.getContext());
                String[] args = { site.getTitle() };
                name = trb.getFormattedMessage("title.resources", args);
            }

            ChefPathItem item = new ChefPathItem(id, name);

            boolean canRead = contentService.allowGetCollection(id) || contentService.allowGetResource(id);
            item.setCanRead(canRead);

            if (canRead) {
                String url = contentService.getUrl(id);
                item.setUrl(url);
            }

            item.setLast(collectionPath.isEmpty());
            if (id.equals(homeCollectionId)) {
                item.setRoot(homeCollectionId);
            } else {
                item.setRoot(navRoot);
            }

            try {
                boolean isFolder = props.getBooleanProperty(ResourceProperties.PROP_IS_COLLECTION);
                item.setIsFolder(isFolder);
            } catch (EntityPropertyNotDefinedException e1) {
            } catch (EntityPropertyTypeException e1) {
            }

            collectionPath.addFirst(item);

        } catch (PermissionException e) {
        } catch (IdUnusedException e) {
        }
    }
    return collectionPath;
}

From source file:hudson.plugins.project_inheritance.projects.InheritanceProject.java

public Graph<SVGNode> getSVGRelationGraph() {
    Graph<SVGNode> out = new Graph<SVGNode>();

    LinkedList<InheritanceProject> open = new LinkedList<InheritanceProject>();
    HashSet<InheritanceProject> visited = new HashSet<InheritanceProject>();

    open.add(this);
    while (!open.isEmpty()) {
        InheritanceProject ip = open.pop();
        if (visited.contains(ip)) {
            continue;
        } else {//from  w ww.  ja v  a2 s .c  om
            visited.add(ip);
        }
        out.addNode(ip);

        for (InheritanceProject parent : ip.getParentProjects()) {
            open.add(parent);
            out.addNode(ip, parent);
        }
    }

    return out;
}

From source file:hudson.plugins.project_inheritance.projects.InheritanceProject.java

public List<InheritedVersionInfo> getAllInheritedVersionsList(InheritanceBuild build) {
    List<InheritedVersionInfo> out = new LinkedList<InheritedVersionInfo>();

    //Adding ourselves as the first entry
    LinkedList<Long> verLst = new LinkedList<Long>();
    for (Version v : this.versionStore.getAllVersions()) {
        verLst.add(v.id);/*from   w  ww. ja  va2 s.c  o  m*/
    }
    if (!verLst.isEmpty()) {
        Long verID = this.getUserDesiredVersion();
        Version verObj = this.versionStore.getVersion(verID);

        out.add(new InheritedVersionInfo(this, verID, verLst,
                (verObj != null) ? verObj.getDescription() : null));
    }

    Map<String, Long> buildVersions = null;
    if (build != null) {
        buildVersions = build.getProjectVersions();
    }

    //Fetching all parent references in order and adding them
    List<AbstractProjectReference> aprLst = this.getAllParentReferences(SELECTOR.MISC);
    for (AbstractProjectReference apr : aprLst) {
        InheritanceProject ip = apr.getProject();
        if (ip == null) {
            continue;
        }

        verLst = new LinkedList<Long>();
        for (Version v : ip.versionStore.getAllVersions()) {
            verLst.add(v.id);
        }
        if (verLst.isEmpty()) {
            // No versions available for that project; skipping it
            continue;
        }

        //Fetch the version; either from the passed build or URL params
        Long verID = ip.getUserDesiredVersion(true);
        if (verID == null) {
            if (buildVersions != null) {
                verID = buildVersions.get(ip.getName());
            } else {
                verID = ip.getUserDesiredVersion();
            }
        }

        if (verID != null) {
            //Fetch the version object associated with the given ID 
            Version verObj = ip.versionStore.getVersion(verID);
            if (verObj == null) {
                continue;
            }

            out.add(new InheritedVersionInfo(ip, verID, verLst, verObj.getDescription()));
        }
    }
    return out;
}

From source file:hudson.plugins.project_inheritance.projects.InheritanceProject.java

/**
 * This method returns the versions selected for this project and its
 * parents.//from   w ww .java  2s. c  o m
 * 
 * @return
 */
public Map<String, Long> getAllVersionsFromCurrentState() {
    LinkedList<InheritanceProject> open = new LinkedList<InheritanceProject>();
    Set<String> closed = new HashSet<String>();
    Map<String, Long> out = new HashMap<String, Long>();

    //Adding ourselves as the first node
    open.add(this);

    while (!open.isEmpty()) {
        InheritanceProject ip = open.pop();
        //Fetching the user-requested version for the open node
        Long v = ip.getUserDesiredVersion();
        out.put(ip.getName(), v);
        //Then, adding this node to the closed set
        closed.add(ip.getName());
        //And adding the parent nodes to the open list
        for (AbstractProjectReference apr : ip.getParentReferences()) {
            if (closed.contains(apr.getName())) {
                continue;
            }
            InheritanceProject next = apr.getProject();
            if (next == null) {
                continue;
            }
            open.addLast(next);
        }
    }
    return out;
}

From source file:hudson.plugins.project_inheritance.projects.InheritanceProject.java

public Map<InheritanceProject, Relationship> getRelationships() {
    Object obj = onInheritChangeBuffer.get(this, "getRelationships");
    if (obj != null && obj instanceof Map) {
        return (Map) obj;
    }//from w ww . j  a  v a  2 s . com

    //Creating the returned map and pre-filling it with empty lists
    Map<InheritanceProject, Relationship> map = new HashMap<InheritanceProject, Relationship>();

    //Preparing the set of projects that were already explored
    HashSet<String> seenProjects = new HashSet<String>();

    //Fetching the map of all projects and their connections
    Map<String, ProjectGraphNode> connGraph = getConnectionGraph();

    //Fetching the node for the current (this) project
    ProjectGraphNode node = connGraph.get(this.getName());
    if (node == null) {
        return map;
    }

    //Mates can be filled quite easily
    for (String mate : node.mates) {
        InheritanceProject p = InheritanceProject.getProjectByName(mate);
        ProjectGraphNode mateNode = connGraph.get(mate);
        boolean isLeaf = (mateNode == null) ? true : mateNode.children.isEmpty();
        if (p == null) {
            continue;
        }
        //Checking if we've seen this mate already
        if (!seenProjects.contains(p.getName())) {
            map.put(p, new Relationship(Relationship.Type.MATE, 0, isLeaf));
            seenProjects.add(p.getName());
        }
    }

    //Exploring parents
    int distance = 1;
    seenProjects.clear();
    LinkedList<InheritanceProject> cOpen = new LinkedList<InheritanceProject>();
    LinkedList<InheritanceProject> nOpen = new LinkedList<InheritanceProject>();
    cOpen.add(this);
    while (!cOpen.isEmpty()) {
        InheritanceProject ip = cOpen.pop();
        if (ip == null || seenProjects.contains(ip.getName())) {
            continue;
        }
        seenProjects.add(ip.getName());

        node = connGraph.get(ip.getName());
        if (ip == null || node == null) {
            continue;
        }
        //Adding all parents
        for (String parent : node.parents) {
            InheritanceProject par = InheritanceProject.getProjectByName(parent);
            if (par == null || seenProjects.contains(parent)) {
                continue;
            }
            map.put(par, new Relationship(Relationship.Type.PARENT, distance, false));
            nOpen.push(par);
        }
        if (cOpen.isEmpty() && !nOpen.isEmpty()) {
            cOpen = nOpen;
            nOpen = new LinkedList<InheritanceProject>();
            distance++;
        }
    }

    //Exploring children
    distance = 1;
    seenProjects.clear();
    cOpen.clear();
    nOpen.clear();
    cOpen.add(this);
    while (!cOpen.isEmpty()) {
        InheritanceProject ip = cOpen.pop();
        if (ip == null || seenProjects.contains(ip.getName())) {
            continue;
        }
        seenProjects.add(ip.getName());

        node = connGraph.get(ip.getName());
        if (ip == null || node == null) {
            continue;
        }
        //Adding all parents
        for (String child : node.children) {
            InheritanceProject cProj = InheritanceProject.getProjectByName(child);
            if (cProj == null || seenProjects.contains(child)) {
                continue;
            }
            ProjectGraphNode childNode = connGraph.get(child);
            boolean isLeaf = (childNode == null) ? true : childNode.children.isEmpty();
            map.put(cProj, new Relationship(Relationship.Type.CHILD, distance, isLeaf));
            nOpen.push(cProj);
        }
        if (cOpen.isEmpty() && !nOpen.isEmpty()) {
            cOpen = nOpen;
            nOpen = new LinkedList<InheritanceProject>();
            distance++;
        }
    }

    onInheritChangeBuffer.set(this, "getRelationships", map);
    return map;
}

From source file:net.spfbl.core.Analise.java

public static void processIP(String ip, StringBuilder builder, int timeout) {
    try {//from w w  w  . ja v  a2 s  .co  m
        ip = Subnet.normalizeIP(ip);
        Distribution dist = SPF.getDistribution(ip, false);
        float probability = dist == null ? 0.0f : dist.getSpamProbability(ip);
        boolean ipv4 = SubnetIPv4.isValidIPv4(ip);
        Object response = null;
        Status statusIP;
        String tokenName;
        Status statusName = Status.NONE;
        LinkedList<String> nameList = new LinkedList<String>();
        try {
            for (String ptr : Reverse.getPointerSet(ip)) {
                nameList.add(ptr);
                if (Generic.containsDynamic(ptr)) {
                    statusName = Status.DYNAMIC;
                    break;
                } else if (Block.containsDomain(ptr, false)) {
                    statusName = Status.BLOCK;
                } else if (Block.containsREGEX(ptr)) {
                    statusName = Status.BLOCK;
                } else if (Block.containsWHOIS(ptr)) {
                    statusName = Status.BLOCK;
                } else if (Generic.containsGeneric(ptr)) {
                    statusName = Status.GENERIC;
                } else {
                    try {
                        if (Reverse.getAddressSet(ptr).contains(ip)) {
                            Distribution distPTR;
                            if (White.containsDomain(ptr)) {
                                statusName = Status.WHITE;
                                break;
                            } else if (Provider.containsDomain(ptr)) {
                                statusName = Status.PROVIDER;
                                break;
                            } else if (Ignore.contains(ptr)) {
                                statusName = Status.IGNORE;
                                break;
                            } else if ((distPTR = SPF.getDistribution(ptr, false)) == null) {
                                statusName = Status.GREEN;
                                break;
                            } else {
                                statusName = Status.valueOf(distPTR.getStatus(ptr).name());
                                break;
                            }
                        } else {
                            statusName = Status.INVALID;
                        }
                    } catch (NamingException ex) {
                        statusName = Status.NXDOMAIN;
                    }
                }
            }
        } catch (CommunicationException ex) {
            statusName = Status.TIMEOUT;
        } catch (ServiceUnavailableException ex) {
            statusName = Status.UNAVAILABLE;
        } catch (NamingException ex) {
            statusName = Status.NONE;
        }
        if (White.containsIP(ip)) {
            statusIP = Status.WHITE;
        } else if (Block.containsCIDR(ip)) {
            statusIP = Status.BLOCK;
        } else if (Provider.containsCIDR(ip)) {
            statusIP = Status.PROVIDER;
        } else if (Ignore.containsCIDR(ip)) {
            statusIP = Status.IGNORE;
        } else if (Block.containsDNSBL(ip)) {
            statusIP = Status.DNSBL;
        } else if (statusName == Status.TIMEOUT && hasAccessSMTP(ip)
                && (response = getResponseSMTP(ip, 25, timeout)) instanceof Status) {
            statusIP = (Status) response;
        } else if (statusName == Status.UNAVAILABLE && hasAccessSMTP(ip)
                && (response = getResponseSMTP(ip, 25, timeout)) instanceof Status) {
            statusIP = (Status) response;
        } else if (statusName == Status.NONE && hasAccessSMTP(ip)
                && (response = getResponseSMTP(ip, 25, timeout)) instanceof Status) {
            statusIP = (Status) response;
        } else if (dist == null) {
            statusIP = Status.GREEN;
        } else {
            statusIP = Status.valueOf(dist.getStatus(ip).name());
        }
        if (response instanceof String) {
            nameList.addLast((String) response);
        }
        if (statusName == Status.TIMEOUT) {
            tokenName = ip;
        } else if (statusName == Status.UNAVAILABLE) {
            tokenName = ip;
        } else if (nameList.isEmpty()) {
            tokenName = ip;
            statusName = Status.NONE;
        } else {
            tokenName = nameList.getFirst();
            statusName = Status.INVALID;
        }
        for (String name : nameList) {
            if (Generic.containsDynamic(name)) {
                tokenName = name;
                statusName = Status.DYNAMIC;
                break;
            } else if (Block.containsDomain(name, false)) {
                tokenName = name;
                statusName = Status.BLOCK;
                break;
            } else if (Block.containsREGEX(name)) {
                tokenName = name;
                statusName = Status.BLOCK;
                break;
            } else if (Block.containsWHOIS(name)) {
                tokenName = name;
                statusName = Status.BLOCK;
                break;
            } else if (Generic.containsGeneric(name)) {
                tokenName = name;
                statusName = Status.GENERIC;
                break;
            } else {
                try {
                    if (Reverse.getAddressSet(name).contains(ip)) {
                        if (White.containsDomain(name)) {
                            tokenName = name;
                            statusName = Status.WHITE;
                            break;
                        } else if (Provider.containsDomain(name)) {
                            tokenName = name;
                            statusName = Status.PROVIDER;
                            break;
                        } else if (Ignore.contains(name)) {
                            tokenName = name;
                            statusName = Status.IGNORE;
                            break;
                        } else {
                            tokenName = name;
                            Distribution distribution2 = SPF.getDistribution(name, false);
                            if (distribution2 == null) {
                                statusName = Status.GREEN;
                            } else {
                                statusName = Status.valueOf(distribution2.getStatus(name).name());
                            }
                        }
                    }
                } catch (NameNotFoundException ex) {
                    tokenName = name;
                    statusName = Status.NXDOMAIN;
                } catch (NamingException ex) {
                    // Fazer nada.
                }
            }
        }
        if (statusName == Status.INVALID || statusName == Status.NXDOMAIN) {
            try {
                String domain = Domain.extractDomain(tokenName, true);
                if (!Reverse.hasValidNameServers(domain)) {
                    if (Block.addExact(domain)) {
                        statusName = Status.BLOCK;
                        Server.logDebug("new BLOCK '" + domain + "' added by 'NXDOMAIN'.");
                        Peer.sendBlockToAll(domain);
                    }
                }
            } catch (NamingException ex) {
                // Fazer nada.
            } catch (ProcessException ex) {
                if (ex.isErrorMessage("RESERVED")) {
                    statusName = Status.RESERVED;
                } else {
                    Server.logError(ex);
                }
            }
        }
        if (statusIP != Status.BLOCK && statusName == Status.DYNAMIC) {
            String token = ip + (SubnetIPv4.isValidIPv4(ip) ? "/24" : "/48");
            String cidr = Subnet.normalizeCIDR(token);
            if (Block.tryOverlap(cidr)) {
                Server.logDebug("new BLOCK '" + token + "' added by '" + tokenName + ";" + statusName + "'.");
            } else if (Block.tryAdd(ip)) {
                Server.logDebug("new BLOCK '" + ip + "' added by '" + tokenName + ";" + statusName + "'.");
            }
            String previous = Subnet.getFirstIP(cidr);
            previous = Subnet.getPreviousIP(previous);
            previous = Subnet.getPreviousIP(previous);
            Analise.processToday(previous);
            String next = Subnet.getLastIP(cidr);
            next = Subnet.getNextIP(next);
            next = Subnet.getNextIP(next);
            Analise.processToday(next);
            statusIP = Status.BLOCK;
        } else if (statusIP != Status.BLOCK && statusName == Status.NONE) {
            String token = ip + (ipv4 ? "/32" : "/64");
            String cidr = Subnet.normalizeCIDR(token);
            if (Block.tryOverlap(cidr)) {
                Server.logDebug("new BLOCK '" + token + "' added by '" + tokenName + ";" + statusName + "'.");
            } else if (Block.tryAdd(ip)) {
                Server.logDebug("new BLOCK '" + ip + "' added by '" + tokenName + ";" + statusName + "'.");
            }
            if (ipv4) {
                cidr = Subnet.normalizeCIDR(ip + "/24");
                String next = Subnet.getFirstIP(cidr);
                for (int index = 0; index < 256; index++) {
                    if (!hasReverse(next) && Block.tryAdd(next)) {
                        Server.logDebug("new BLOCK '" + next + "' added by '" + next + ";" + statusName + "'.");
                    }
                    next = Subnet.getNextIP(next);
                }
            }
            statusIP = Status.BLOCK;
        } else if (statusIP != Status.BLOCK && (statusName == Status.BLOCK || statusName == Status.RESERVED
                || statusName == Status.NXDOMAIN)) {
            if (Block.tryAdd(ip)) {
                Server.logDebug("new BLOCK '" + ip + "' added by '" + tokenName + ";" + statusName + "'.");
            }
            statusIP = Status.BLOCK;
        } else if (statusIP != Status.BLOCK && statusIP != Status.IGNORE && statusName != Status.PROVIDER
                && statusName != Status.IGNORE && statusName != Status.GREEN && statusName != Status.WHITE
                && SubnetIPv6.isSLAAC(ip)) {
            String token = ip + "/64";
            String cidr = SubnetIPv6.normalizeCIDRv6(token);
            if (Block.tryOverlap(cidr)) {
                Server.logDebug("new BLOCK '" + token + "' added by 'SLAAC'.");
            } else if (Block.tryAdd(ip)) {
                Server.logDebug("new BLOCK '" + ip + "' added by 'SLAAC'.");
            }
            statusIP = Status.BLOCK;
            //            } else if (statusIP != Status.BLOCK && statusIP != Status.IGNORE && statusIP != Status.WHITE && statusName != Status.PROVIDER && statusName != Status.IGNORE && statusName != Status.WHITE && isCusterRED(ip, null, tokenName)) {
            //                if (Block.tryAdd(ip)) {
            //                    Server.logDebug("new BLOCK '" + ip + "' added by '" + tokenName + ";CLUSTER'.");
            //                }
            //                statusIP = Status.BLOCK;
        } else if (statusIP == Status.DNSBL && (statusName != Status.GREEN && statusName != Status.PROVIDER
                && statusName != Status.IGNORE && statusName != Status.WHITE)) {
            if (Block.tryAdd(ip)) {
                Server.logDebug("new BLOCK '" + ip + "' added by '" + tokenName + ";" + statusIP + "'.");
            }
            statusIP = Status.BLOCK;
        } else if (statusIP == Status.CLOSED && statusName == Status.RED) {
            if (Block.tryAdd(ip)) {
                Server.logDebug("new BLOCK '" + ip + "' added by '" + tokenName + ";" + statusIP + "'.");
            }
            statusIP = Status.BLOCK;
        } else if (statusIP != Status.BLOCK && statusName == Status.INVALID
                && Generic.containsGenericDomain(tokenName)) {
            if (Block.tryAdd(ip)) {
                Server.logDebug("new BLOCK '" + ip + "' added by '" + tokenName + ";" + statusName + "'.");
            }
            statusIP = Status.BLOCK;
        } else if ((statusName == Status.INVALID || statusName == Status.GENERIC)
                && (statusIP == Status.CLOSED || statusIP == Status.RED || statusIP == Status.YELLOW)) {
            if (Block.tryAdd(ip)) {
                Server.logDebug("new BLOCK '" + ip + "' added by '" + tokenName + ";" + statusName + "'.");
            }
            statusIP = Status.BLOCK;
        } else if (statusIP == Status.BLOCK && (statusName == Status.YELLOW || statusName == Status.RED)) {
            if (Block.tryAdd(tokenName)) {
                Server.logDebug(
                        "new BLOCK '" + tokenName + "' added by '" + tokenName + ";" + statusName + "'.");
            }
            statusName = Status.BLOCK;
        } else if (statusIP == Status.BLOCK && (statusName == Status.PROVIDER || statusName == Status.IGNORE
                || statusName == Status.WHITE)) {
            String cidr;
            int mask = SubnetIPv4.isValidIPv4(ip) ? 32 : 64;
            if ((cidr = Block.clearCIDR(ip, mask)) != null) {
                Server.logInfo("false positive BLOCK '" + cidr + "' detected by '" + tokenName + ";"
                        + statusName + "'.");
            }
            if (Provider.containsCIDR(ip)) {
                statusIP = Status.PROVIDER;
            } else if (Ignore.containsCIDR(ip)) {
                statusIP = Status.IGNORE;
            } else if (Block.containsDNSBL(ip)) {
                statusIP = Status.DNSBL;
            } else if (hasAccessSMTP(ip) && (response = getResponseSMTP(ip, 25, timeout)) instanceof Status) {
                statusIP = (Status) response;
            } else if (dist == null) {
                statusIP = Status.GREEN;
            } else {
                statusIP = Status.valueOf(dist.getStatus(ip).name());
            }
        } else if (statusIP == Status.DNSBL && (statusName == Status.PROVIDER || statusName == Status.IGNORE
                || statusName == Status.WHITE)) {
            if (hasAccessSMTP(ip) && (response = getResponseSMTP(ip, 25, timeout)) instanceof Status) {
                statusIP = (Status) response;
            } else if (dist == null) {
                statusIP = Status.GREEN;
            } else {
                statusIP = Status.valueOf(dist.getStatus(ip).name());
            }
        }
        builder.append(statusIP);
        builder.append(' ');
        builder.append(tokenName);
        builder.append(' ');
        builder.append(statusName);
        builder.append(' ');
        builder.append(Core.DECIMAL_FORMAT.format(probability));
        builder.append(' ');
        builder.append(dist == null ? "UNDEFINED" : dist.getFrequencyLiteral());
        builder.append(' ');
        if (Subnet.isValidIP(tokenName)) {
            builder.append(Subnet.expandIP(tokenName));
        } else {
            builder.append(Domain.revert(tokenName));
            addCluster(convertHostToMask(tokenName), statusName, dist);
            addCluster(extractTLD(tokenName), statusName, dist);
            addCluster(getOwnerID(tokenName), statusName, dist);
        }
        addCluster(Subnet.normalizeCIDR(ip + (ipv4 ? "/24" : "/56")), statusIP, dist);
    } catch (Exception ex) {
        builder.append("ERROR");
        Server.logError(ex);
    }
}