Example usage for java.util HashSet isEmpty

List of usage examples for java.util HashSet isEmpty

Introduction

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

Prototype

public boolean isEmpty() 

Source Link

Document

Returns true if this set contains no elements.

Usage

From source file:org.deegree.feature.persistence.shape.ShapeFeatureStore.java

@Override
public FeatureInputStream query(Query query) throws FilterEvaluationException, FeatureStoreException {

    if (query.getTypeNames() == null || query.getTypeNames().length > 1) {
        String msg = "Only queries with exactly one or zero type name(s) are supported.";
        throw new UnsupportedOperationException(msg);
    }/*from  ww w.  j av  a2s.  c  o m*/

    HashSet<Integer> idFilterNums = null;
    if (query.getFilter() instanceof IdFilter) {
        idFilterNums = new HashSet<Integer>();
        IdFilter f = (IdFilter) query.getFilter();
        List<ResourceId> ids = f.getSelectedIds();
        for (ResourceId id : ids) {
            if (id.getRid().startsWith(fidPrefix)) {
                String[] ss = id.getRid().split("_");
                idFilterNums.add(Integer.valueOf(ss[1]));
            }
        }
    }

    if (query.getTypeNames().length == 0 && !(query.getFilter() instanceof IdFilter)
            || idFilterNums != null && idFilterNums.isEmpty()) {
        return new MemoryFeatureInputStream(new GenericFeatureCollection());
    }

    if (query.getTypeNames().length > 0) {
        QName featureType = query.getTypeNames()[0].getFeatureTypeName();
        if (featureType != null && !featureType.equals(ft.getName())) {
            // or null?
            return new MemoryFeatureInputStream(new GenericFeatureCollection());
        }
    }

    checkForUpdate();

    if (!available) {
        return null;
    }

    Filter filter = query.getFilter();
    Pair<Filter, Envelope> filterPair = splitOffBBoxConstraint(filter);

    List<Pair<Integer, Long>> recNumsAndPos = new LinkedList<Pair<Integer, Long>>();
    Envelope bbox = getTransformedEnvelope(query.getPrefilterBBoxEnvelope());

    if (bbox == null) {
        getEnvelope(null);
    }

    boolean queryIndex = filterPair.first == null || !generateAlphanumericIndexes;
    Pair<Filter, SortProperty[]> p = queryIndex ? null
            : dbfIndex.query(recNumsAndPos, filterPair.first, query.getSortProperties());
    HashSet<Integer> recNums = new HashSet<Integer>(unzipPair(recNumsAndPos).first);
    if (idFilterNums != null) {
        recNums.addAll(idFilterNums);
    }
    recNumsAndPos = shp.query(bbox, filter == null || p == null ? null : recNums);
    LOG.debug("{} records matching after BBOX filtering", recNumsAndPos.size());

    // don't forget about filters if dbf index could not be queried
    if (p == null) {
        p = new Pair<Filter, SortProperty[]>(filterPair.first, query.getSortProperties());
    }

    FeatureInputStream rs = new IteratorFeatureInputStream(new FeatureIterator(recNumsAndPos.iterator()));

    if (p.first != null) {
        LOG.debug("Applying in-memory filtering.");
        rs = new FilteredFeatureInputStream(rs, p.first);
    }

    if (p.second != null && p.second.length > 0) {
        LOG.debug("Applying in-memory sorting.");
        rs = new MemoryFeatureInputStream(Features.sortFc(rs.toCollection(), p.second));
    }

    return rs;
}

From source file:com.example.app.profile.ui.user.ProfileMembershipManagement.java

@Override
public void init() {
    super.init();
    setValidator(CompositeValidator.of((component, notifiable) -> validateSupporters(notifiable),
            (component, notifiable) -> {
                if (_requiredMembershipTypes.isEmpty())
                    return true;
                final HashSet<MembershipType> toCheck = new HashSet<>(_requiredMembershipTypes);
                getProfile().getMembershipSet()
                        .forEach(membership -> toCheck.remove(membership.getMembershipType()));
                toCheck.forEach(mt -> {
                    NotificationImpl notification = new NotificationImpl(NotificationType.ERROR,
                            createText(CommonValidationText.ARG0_IS_REQUIRED, mt.getName()));
                    notification.setSource(this);
                    notifiable.sendNotification(notification);
                });/*w  ww.j  a v  a2s. c  o  m*/
                return toCheck.isEmpty();
            }));

    User currentUser = _userDAO.getAssertedCurrentUser();
    Hibernate.initialize(currentUser);
    Hibernate.initialize(currentUser.getPrincipal());
    Hibernate.initialize(currentUser.getPrincipal().getContact());
    final Profile adminProfile = getProfile();
    final TimeZone timeZone = getSession().getTimeZone();
    boolean isAdminish = _profileDAO.canOperate(currentUser, adminProfile, timeZone, _mop.modifyCompany());
    if (!_profileDAO.canOperate(currentUser, adminProfile, timeZone, _mop.modifyUserRoles())) {
        Label label = new Label(INSUFFICIENT_PERMISSIONS(MEMBERSHIP())).withHTMLElement(HTMLElement.h3);
        setDefaultComponent(label);
        return;
    }
    final SimpleDateFormat dateFormat = getDateFormat(getLocaleContext().getLocale());
    dateFormat.setTimeZone(getSession().getTimeZone());
    final DateFormatLabel dateRenderer = new DateFormatLabel(dateFormat) {
        @Override
        public Component getTableCellRendererComponent(Table table, Object cellValue, boolean isSelected,
                boolean hasFocus, int row, int column) {
            Date value = (Date) cellValue;
            value = toDate(convertFromPersisted(value, getSession().getTimeZone()));
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    };
    final NamedObjectComparator nocComparator = new NamedObjectComparator(getLocaleContext());
    FixedValueColumn actionColumn = new FixedValueColumn();
    actionColumn.setColumnName(CommonColumnText.ACTIONS);
    PropertyColumn userColumn = new PropertyColumn(Membership.class, Membership.USER_PROP);
    userColumn.setColumnName(CommonColumnText.USER);
    userColumn.setComparator(nocComparator);
    PropertyColumn membershipTypeColumn = new PropertyColumn(Membership.class, Membership.MEMBERSHIP_TYPE_PROP);
    membershipTypeColumn.setColumnName(MEMBERSHIP_TYPE());
    membershipTypeColumn.setComparator(nocComparator);
    PropertyColumn membershipDStartColumn = new PropertyColumn(Membership.class, Membership.START_DATE_PROP);
    membershipDStartColumn.setColumnName(START_DATE());
    membershipDStartColumn.setComparator(ComparableComparator.getInstance());
    PropertyColumn membershipDEndColumn = new PropertyColumn(Membership.class, Membership.END_DATE_PROP);
    membershipDEndColumn.setComparator(ComparableComparator.getInstance());
    membershipDEndColumn.setColumnName(END_DATE());

    _membershipTable = isAllowEditActive()
            ? new DataColumnTable<>(actionColumn, userColumn, membershipTypeColumn, membershipDStartColumn,
                    membershipDEndColumn)
            : new DataColumnTable<>(actionColumn, userColumn, membershipTypeColumn);
    _membershipTable.setTableCellRenderer(dateRenderer, Date.class);
    _membershipTable.getDefaultModel().setAutoReattachEntities(false);
    _membershipTable.setRowModel(new RowModelImpl() {
        @Override
        public Row getRow(Table table, int row) {
            final Row r = super.getRow(table, row);
            final Membership membership = _membershipTable.getDefaultModel().getRow(row);
            if (membership.isActive()) {
                r.removeClassName("member-inactive");
                r.addClassName("member-active");
            } else {
                r.addClassName("member-inactive");
                r.removeClassName("member-active");
            }
            return r;
        }
    });

    QLResolverOptions resolverOptions = new QLResolverOptions();
    resolverOptions.setFetchSize(1);
    resolverOptions.setCacheRegion(ProjectCacheRegions.MEMBER_QUERY);

    PushButton editOperationsBtn = new PushButton(BUTTON_TEXT_MODIFY()) {
        @Override
        public Component getTableCellRendererComponent(Table table, @Nullable Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            Membership mem = (Membership) value;
            boolean hasOperations = false;
            if (mem != null) {
                final QLBuilderImpl qb = new QLBuilderImpl(ProfileType.class, "ptAlias");
                qb.setQLResolverOptions(resolverOptions);
                qb.appendCriteria("id", PropertyConstraint.Operator.eq,
                        mem.getProfile().getProfileType().getId());
                final JoinedQLBuilder mtQB = qb.createInnerJoin(ProfileType.MEMBERSHIP_TYPES_PROP);
                final JoinedQLBuilder opQB = mtQB.createInnerJoin(MembershipType.DEFAULT_OPERATIONS_PROP);
                qb.setProjection("COUNT(DISTINCT %s)", opQB.getAlias());

                try (CloseableIterator<?> it = qb.getQueryResolver().iterate()) {
                    if (it.hasNext()) {
                        final Number next = (Number) it.next();
                        hasOperations = next.intValue() > 0;
                    }
                }
            }
            setVisible(hasOperations && isAdminish);

            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    };
    editOperationsBtn.addActionListener(eev -> doOperationEdit(_membershipTable.getLeadSelection()));
    PushButton editActivationDatesBtn = new PushButton(ACTION_EDIT_DATES());
    editActivationDatesBtn.setTooltip(TOOLTIP_EDIT_DATES(MEMBERSHIP()));
    editActivationDatesBtn.addActionListener(eev -> doDatesEdit(_membershipTable.getLeadSelection()));

    PushButton deactivateBtn = new PushButton(ACTION_DEACTIVATE()) {
        @Override
        public Component getTableCellRendererComponent(Table table, @Nullable Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            Membership m = (Membership) value;
            assert m != null;
            setVisible(m.getEndDate() == null);

            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    };
    deactivateBtn.setTooltip(TOOLTIP_DEACTIVATE());
    deactivateBtn.addActionListener(ev -> {
        final Membership membership = _membershipTable.getLeadSelection();
        assert membership != null;
        final Date now = convertForPersistence(
                getZonedDateTimeForComparison(timeZone).minus(1, ChronoUnit.DAYS));
        membership.setEndDate(now);
        showHideConstraints();
    });

    PushButton deleteBtn = CommonActions.DELETE.push();
    deleteBtn.getButtonDisplay().setConfirmText(DELETE_CONFIRM_TEXT_FMT(MEMBERSHIP()));
    deleteBtn.addActionListener(dev -> {
        final Membership membership = _membershipTable.getLeadSelection();
        assert membership != null;
        membership.getProfile().getMembershipSet().remove(membership);
        reloadTableData();
    });
    Container actions = of("actions", editOperationsBtn);
    if (isAllowEditActive()) {
        actions.add(editActivationDatesBtn);
        actions.add(deactivateBtn);
    }
    if (isAdminish)
        actions.add(deleteBtn);
    final Column uiColumn = _membershipTable.getUIColumn(actionColumn);
    assert uiColumn != null;
    uiColumn.setDisplayClass("action-column");
    _membershipTable.setUICellRenderer(actionColumn, actions);

    Menu menu = new Menu(CommonButtonText.ADD);
    menu.setTooltip(ConcatTextSource.create(CommonButtonText.ADD, MEMBERSHIP_TYPE()).withSpaceSeparator());
    enableTooltip(menu);
    menu.addClassName("entity-action");
    LocaleContext lc = getLocaleContext();
    getProfile().getProfileType().getMembershipTypeSet().stream()
            .filter(membershipType -> !_excludedMembershipTypes.contains(membershipType))
            .sorted(new NamedObjectComparator(lc)).forEach(mt -> {
                TextSource menuItemText = mt.getName();
                MenuItem mi = new MenuItem(menuItemText);
                mi.addActionListener(ev -> doSelectUserAndCreateMembership(mt));
                menu.add(mi);
            });

    _activeConstraint.setSelectedObject(ACTIVE());
    _activeConstraint.addActionListener(this::reloadTableData);

    setDefaultComponent(of("search-wrapper profile-role-search", of("entity-actions actions", menu),
            of("search-bar", _activeConstraint), new Pager(_membershipTable.addClassName("search-results"))));

    reloadTableData();
}

From source file:org.sakaiproject.memory.impl.MemCache.java

@Override
public void putAll(Map<String, Object> map) {
    if (map != null && !map.isEmpty()) {
        HashSet<Element> elements = new HashSet<Element>(map.size());
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getKey() != null) {
                elements.add(new Element(entry.getKey(), entry.getValue()));
            }/*w w w  .j  ava2s . c o  m*/
        }
        if (!elements.isEmpty()) {
            cache.putAll(elements);
        }
    }
}

From source file:au.org.ala.delta.intkey.model.IntkeyDatasetFileReader.java

/**
 * Read attributes from the items file/*from  ww  w .  j  a v  a 2s  .  c  o m*/
 * 
 * @param itemFileHeader
 *            item file header
 * @param itemBinFile
 *            item file data
 * @param c
 *            character that we want attributes for
 * @param taxa
 *            taxa that we want attributes for
 * @return a list of attributes for the supplied character and taxa.
 */
private static List<Attribute> readAttributes(ItemsFileHeader itemFileHeader, BinFile itemBinFile, Character c,
        List<Item> taxa) {
    List<Attribute> retList = new ArrayList<Attribute>();

    int totalNumChars = itemFileHeader.getNChar();
    int totalNumTaxa = itemFileHeader.getNItem();

    seekToRecord(itemBinFile, itemFileHeader.getRpCdat());
    List<Integer> charAttributeDataRecordIndicies = readIntegerList(itemBinFile, totalNumChars);

    // Subtract 1 from the charNo because characters are zero indexed in
    // intkey API
    int charNo = c.getCharacterId();
    int charTaxonDataRecordIndex = charAttributeDataRecordIndicies.get(charNo - 1);

    seekToRecord(itemBinFile, charTaxonDataRecordIndex);

    if (c instanceof MultiStateCharacter) {

        MultiStateCharacter multiStateChar = (MultiStateCharacter) c;

        int bitsPerTaxon = multiStateChar.getStates().length + 1;
        int totalBitsNeeded = bitsPerTaxon * totalNumTaxa;
        int bytesToRead = Double.valueOf(Math.ceil(Double.valueOf(totalBitsNeeded) / Double.valueOf(Byte.SIZE)))
                .intValue();

        byte[] bytes = new byte[bytesToRead];
        itemBinFile.readBytes(bytes);
        boolean[] taxaData = Utils.byteArrayToBooleanArray(bytes);

        for (Item t : taxa) {
            int startIndex = (t.getItemNumber() - 1) * bitsPerTaxon; // Taxa
                                                                     // numbers
                                                                     // are
                                                                     // 1
                                                                     // indexed
                                                                     // instead
                                                                     // of 0
                                                                     // indexed
            int endIndex = startIndex + bitsPerTaxon;

            boolean[] taxonData = Arrays.copyOfRange(taxaData, startIndex, endIndex);

            // Taxon data consists of a bit for each state, indicating
            // the states presence, followed by
            // a final bit signifying whether or not the character is
            // inapplicable for the taxon.
            boolean inapplicable = taxonData[taxonData.length - 1];

            HashSet<Integer> presentStates = new HashSet<Integer>();
            for (int k = 0; k < taxonData.length - 1; k++) {
                boolean statePresent = taxonData[k];
                if (statePresent) {
                    presentStates.add(k + 1);
                }
            }

            SimpleAttributeData attrData = new SimpleAttributeData(presentStates.isEmpty(), inapplicable);
            MultiStateAttribute msAttr = new MultiStateAttribute(multiStateChar, attrData);
            msAttr.setItem(t);

            msAttr.setPresentStates(presentStates);

            retList.add(msAttr);
        }

    } else if (c instanceof IntegerCharacter) {
        IntegerCharacter intChar = (IntegerCharacter) c;
        int charMinValue = intChar.getMinimumValue();
        int charMaxValue = intChar.getMaximumValue();

        // 1 bit for all values below minimum, 1 bit for each value between
        // minimum and maximum (inclusive),
        // 1 bit for all values above maximum, 1 inapplicability bit.
        int bitsPerTaxon = charMaxValue - charMinValue + 4;
        int totalBitsNeeded = bitsPerTaxon * totalNumTaxa;

        int bytesToRead = Double.valueOf(Math.ceil(Double.valueOf(totalBitsNeeded) / Double.valueOf(Byte.SIZE)))
                .intValue();

        byte[] bytes = new byte[bytesToRead];
        itemBinFile.readBytes(bytes);
        boolean[] taxaData = Utils.byteArrayToBooleanArray(bytes);

        for (Item t : taxa) {
            int startIndex = (t.getItemNumber() - 1) * bitsPerTaxon; // Taxa
                                                                     // numbers
                                                                     // are
                                                                     // 1
                                                                     // indexed
                                                                     // instead
                                                                     // of 0
                                                                     // indexed
            int endIndex = startIndex + bitsPerTaxon;

            boolean[] taxonData = Arrays.copyOfRange(taxaData, startIndex, endIndex);

            boolean inapplicable = taxonData[taxonData.length - 1];

            Set<Integer> presentValues = new HashSet<Integer>();
            for (int k = 0; k < taxonData.length - 1; k++) {
                boolean present = taxonData[k];
                if (present) {
                    presentValues.add(k + charMinValue - 1);
                }
            }

            IntegerAttribute intAttr = new IntegerAttribute(intChar,
                    new SimpleAttributeData(presentValues.isEmpty(), inapplicable));
            intAttr.setItem(t);
            intAttr.setPresentValues(presentValues);

            retList.add(intAttr);
        }

    } else if (c instanceof RealCharacter) {
        // Read NI inapplicability bits
        int bytesToRead = Double.valueOf(Math.ceil(Double.valueOf(totalNumTaxa) / Double.valueOf(Byte.SIZE)))
                .intValue();
        byte[] bytes = new byte[bytesToRead];
        itemBinFile.readBytes(bytes);
        boolean[] taxaInapplicabilityData = Utils.byteArrayToBooleanArray(bytes);

        int recordsSpannedByInapplicabilityData = recordsSpannedByBytes(bytesToRead);

        seekToRecord(itemBinFile, charTaxonDataRecordIndex + recordsSpannedByInapplicabilityData);

        // Read two float values per taxon
        List<Float> taxonData = readFloatList(itemBinFile, totalNumTaxa * 2);

        for (Item t : taxa) {
            int taxonNumber = t.getItemNumber();

            float lowerFloat = taxonData.get((taxonNumber - 1) * 2);
            float upperFloat = taxonData.get(((taxonNumber - 1) * 2) + 1);

            boolean inapplicable = taxaInapplicabilityData[taxonNumber - 1];

            // Character is unknown for the corresponding taxon if
            // lowerfloat > upperfloat
            boolean unknown = lowerFloat > upperFloat;

            RealAttribute realAttr = new RealAttribute((RealCharacter) c,
                    new SimpleAttributeData(unknown, inapplicable));

            if (!unknown) {
                FloatRange range = new FloatRange(lowerFloat, upperFloat);
                realAttr.setPresentRange(range);
            }
            realAttr.setItem(t);

            retList.add(realAttr);
        }

    } else if (c instanceof TextCharacter) {
        TextCharacter textChar = (TextCharacter) c;

        // Read NI inapplicability bits
        int bytesToRead = Double.valueOf(Math.ceil(Double.valueOf(totalNumTaxa) / Double.valueOf(Byte.SIZE)))
                .intValue();
        byte[] bytes = new byte[bytesToRead];
        itemBinFile.readBytes(bytes);
        boolean[] taxaInapplicabilityData = Utils.byteArrayToBooleanArray(bytes);

        int recordsSpannedByInapplicabilityData = recordsSpannedByBytes(bytesToRead);

        seekToRecord(itemBinFile, charTaxonDataRecordIndex + recordsSpannedByInapplicabilityData);

        List<Integer> taxonTextDataOffsets = readIntegerList(itemBinFile, totalNumTaxa + 1);

        int recordsSpannedByOffsets = recordsSpannedByBytes((totalNumTaxa + 1) * Constants.SIZE_INT_IN_BYTES);

        seekToRecord(itemBinFile,
                charTaxonDataRecordIndex + recordsSpannedByInapplicabilityData + recordsSpannedByOffsets);

        ByteBuffer taxonTextData = itemBinFile.readByteBuffer(
                taxonTextDataOffsets.get(taxonTextDataOffsets.size() - taxonTextDataOffsets.get(0)));

        for (Item t : taxa) {
            int taxonNumber = t.getItemNumber();

            int lowerOffset = taxonTextDataOffsets.get(taxonNumber - 1);
            int upperOffset = taxonTextDataOffsets.get((taxonNumber - 1) + 1);
            int textLength = upperOffset - lowerOffset;

            String txt = "";
            if (textLength > 0) {
                byte[] textBytes = new byte[textLength];
                taxonTextData.position(lowerOffset - 1);
                taxonTextData.get(textBytes);

                txt = BinFileEncoding.decode(textBytes);
            }

            boolean inapplicable = taxaInapplicabilityData[taxonNumber - 1];
            boolean unknown = StringUtils.isEmpty(txt);

            TextAttribute txtAttr = new TextAttribute(textChar, new SimpleAttributeData(unknown, inapplicable));
            try {
                txtAttr.setText(txt);
            } catch (DirectiveException e) {
                // The SimpleAttributeData implementation won't throw this
                // Exception.
            }
            txtAttr.setItem(t);

            retList.add(txtAttr);

        }
    }

    return retList;
}

From source file:gr.demokritos.iit.cru.creativity.reasoning.diagrammatic.DiagrammaticComputationalTools.java

public HashSet<String> FindRelations(ArrayList<String> tri, int difficulty, String category)
        throws InstantiationException, IllegalAccessException, SQLException, ClassNotFoundException, Exception {
    HashSet<String> relations = new HashSet<String>();
    HashSet<String> TempRelations = new HashSet<String>();
    HashSet<String> newConcepts = new HashSet<String>();
    ArrayList<String> triple = new ArrayList<String>();

    ///---------translate
    for (String s : tri) {
        String h = Translator.bingTranslate(s, this.language, "en", "general");
        if (wn.getCommonPos(h) == null) {
            // System.out.println(s + " out " + h);
            return newConcepts; //if any element of the triple cannot be translated, relations cannot be found
        }//from   www. j  a  v a 2  s . co m
        triple.add(h);
    }
    // System.out.println(triple);
    //run abstraction for relation to find new relations and work with them
    String relation = triple.get(2);
    for (int i = 0; i < difficulty; i++) {
        TempRelations = ConceptGraphAbstractionEngine(relation);
        if (TempRelations.isEmpty()) {
            break;
        }
        relations = TempRelations;
        int pointer = new Random().nextInt(relations.size());
        int c = 0;
        for (String k : relations) {
            if (c == pointer) {
                relation = k;
            }
            c = c + 1;
        }
    }
    //take the similar concepts to the subject and the object of the triple
    if (category.equalsIgnoreCase("subsumption")) {
        newConcepts = ConceptGraphPolymerismEngine(triple.get(0));//wn.getMeronyms(triple.get(0));
        newConcepts.addAll(ConceptGraphPolymerismEngine(triple.get(1)));//wn.getMeronyms(triple.get(1));
    } else if (category.equalsIgnoreCase("supersumption")) {
        newConcepts = ConceptGraphAbstractionEngine(triple.get(0));// wn.getHyponymsAndHypernyms(triple.get(0));
        newConcepts.addAll(ConceptGraphAbstractionEngine(triple.get(1)));//wn.getHyponymsAndHypernyms(triple.get(1)));
    }
    System.out.println("new concepts " + newConcepts);
    //take concepts that have the relations found
    //  System.out.println("newConc " + newConcepts);
    for (String r : relations) {
        System.out.println(r);
        //System.out.println("relations " + r);
        HashSet<String> temp = FactRetriever(r, "relation");
        for (String g : temp) {
            //keep the subject and the object of the triples
            newConcepts.add(g.split("---")[0]);
            newConcepts.add(g.split("---")[1]);///////////////!!!!!!check if subj/obj=''
        }
    }

    //find relations based on the new concepts
    HashSet<String> newRelations = new HashSet<String>();
    for (String s : newConcepts) {
        // System.out.println("concepts " + s);
        newRelations.addAll(FactRetriever(s, "object"));
        newRelations.addAll(FactRetriever(s, "subject"));
    }
    // System.out.println("newRels " + newRelations);
    /*
     HashSet<String> rels = new HashSet<String>();
     ///---------translate
     if (!this.language.equalsIgnoreCase("en")) {
     for (String s : newRelations) {
     System.out.println(s);
     String n = Translator.bingTranslate(s, "en", this.language, "general");
     if (wn.getCommonPos(n) == null) {//if the word is not english
     rels.add(n);
     }
     }
     } else {
     rels.addAll(newRelations);
     }*/
    return newRelations;
}

From source file:com.ikanow.aleph2.search_service.elasticsearch.services.ElasticsearchIndexService.java

@Override
public Tuple2<String, List<BasicMessageBean>> validateSchema(final SearchIndexSchemaBean schema,
        final DataBucketBean bucket) {
    final LinkedList<BasicMessageBean> errors = new LinkedList<BasicMessageBean>(); // (Warning mutable code)
    try {//from w  w  w  .  j  a  va  2 s  .c  o m
        Map<String, DataSchemaBean.ColumnarSchemaBean> tokenization_overrides = Optionals
                .of(() -> schema.tokenization_override()).orElse(Collections.emptyMap());
        final HashSet<String> unsupported_tokenization_overrides = new HashSet<String>(
                tokenization_overrides.keySet());
        unsupported_tokenization_overrides
                .removeAll(Arrays.asList(ElasticsearchIndexUtils.DEFAULT_TOKENIZATION_TYPE,
                        ElasticsearchIndexUtils.NO_TOKENIZATION_TYPE));
        if (!unsupported_tokenization_overrides.isEmpty()) {
            errors.add(ErrorUtils.buildErrorMessage(bucket.full_name(), "validateSchema",
                    SearchIndexErrorUtils.NOT_YET_SUPPORTED,
                    "tokenization_overrides: " + unsupported_tokenization_overrides.toString()));
        }
        Map<String, DataSchemaBean.ColumnarSchemaBean> type_overrides = Optionals
                .of(() -> schema.type_override()).orElse(Collections.emptyMap());
        type_overrides.keySet().stream().filter(type -> !_supported_types.contains(type))
                .forEach(type -> errors.add(ErrorUtils.buildErrorMessage(bucket.full_name(), "validateSchema",
                        SearchIndexErrorUtils.NOT_YET_SUPPORTED, "type: " + type)));

        // If the user is trying to override the index name then they have to be admin:
        final Optional<String> manual_index_name = Optionals
                .<String>of(() -> ((String) bucket.data_schema().search_index_schema()
                        .technology_override_schema().get(SearchIndexSchemaDefaultBean.index_name_override_)));

        if (manual_index_name.isPresent()) { // (then must be admin)
            if (!_service_context.getSecurityService().hasUserRole(bucket.owner_id(),
                    ISecurityService.ROLE_ADMIN)) {
                errors.add(ErrorUtils.buildErrorMessage(bucket.full_name(), "validateSchema",
                        SearchIndexErrorUtils.NON_ADMIN_BUCKET_NAME_OVERRIDE));
            }
        }

        final String index_name = ElasticsearchIndexUtils.getBaseIndexName(bucket, Optional.empty());
        boolean error = false; // (Warning mutable code)
        final boolean is_verbose = is_verbose(schema);
        final ElasticsearchIndexServiceConfigBean schema_config = ElasticsearchIndexConfigUtils
                .buildConfigBeanFromSchema(bucket, _config, _mapper);

        // 1) Check the schema:

        try {
            final Optional<String> type = Optional.ofNullable(schema_config.search_technology_override())
                    .map(t -> t.type_name_or_prefix());
            final String index_type = CollidePolicy.new_type == Optional
                    .ofNullable(schema_config.search_technology_override()).map(t -> t.collide_policy())
                    .orElse(CollidePolicy.new_type) ? "_default_"
                            : type.orElse(ElasticsearchIndexServiceConfigBean.DEFAULT_FIXED_TYPE_NAME);

            final XContentBuilder mapping = ElasticsearchIndexUtils.createIndexMapping(bucket, Optional.empty(),
                    true, schema_config, _mapper, index_type);
            if (is_verbose) {
                errors.add(ErrorUtils.buildSuccessMessage(bucket.full_name(), "validateSchema",
                        mapping.bytes().toUtf8()));
            }
        } catch (Throwable e) {
            errors.add(ErrorUtils.buildErrorMessage(bucket.full_name(), "validateSchema",
                    ErrorUtils.getLongForm("{0}", e)));
            error = true;
        }

        // 2) Sanity check the max size

        final Optional<Long> index_max_size = Optional
                .ofNullable(schema_config.search_technology_override().target_index_size_mb());
        if (index_max_size.isPresent()) {
            final long max = index_max_size.get();
            if ((max > 0) && (max < 25)) {
                errors.add(ErrorUtils.buildErrorMessage(bucket.full_name(), "validateSchema",
                        SearchIndexErrorUtils.INVALID_MAX_INDEX_SIZE, max));
                error = true;
            } else if (is_verbose) {
                errors.add(ErrorUtils.buildSuccessMessage(bucket.full_name(), "validateSchema",
                        "Max index size = {0} MB", max));
            }
        }
        return Tuples._2T(error ? "" : index_name, errors);
    } catch (Exception e) { // Very early error has occurred, just report that:
        return Tuples._2T("", Arrays.asList(ErrorUtils.buildErrorMessage(bucket.full_name(), "validateSchema",
                ErrorUtils.getLongForm("{0}", e))));
    }
}

From source file:com.linkedin.databus.core.TestDbusEventBufferPersistence.java

@Test
public void testMetaFileCloseMult() throws Exception {
    int maxEventBufferSize = 1144;
    int maxIndividualBufferSize = 500;
    int bufNum = maxEventBufferSize / maxIndividualBufferSize;
    if (maxEventBufferSize % maxIndividualBufferSize > 0)
        bufNum++;//from   w  ww . ja  va2  s . co m

    DbusEventBuffer.StaticConfig config = getConfig(maxEventBufferSize, maxIndividualBufferSize, 100, 500,
            AllocationPolicy.MMAPPED_MEMORY, _mmapDirStr, true);

    // create buffer mult
    DbusEventBufferMult bufMult = createBufferMult(config);

    // Save all the files and validate the meta files.
    bufMult.close();
    for (DbusEventBuffer dbusBuf : bufMult.bufIterable()) {
        File metaFile = new File(_mmapDir, dbusBuf.metaFileName());
        // check that we don't have the files
        Assert.assertTrue(metaFile.exists());
        validateFiles(metaFile, bufNum);
    }
    File[] entries = _mmapDir.listFiles();

    // When we create a new multi-buffer, we should get renamed files as well as new files.
    bufMult = createBufferMult(config);
    entries = _mmapDir.listFiles(); // Has session dirs and renamed meta files.
    // Create an info file for one buffer.
    DbusEventBuffer buf = bufMult.bufIterable().iterator().next();
    buf.saveBufferMetaInfo(true);
    File infoFile = new File(_mmapDir, buf.metaFileName() + ".info");
    Assert.assertTrue(infoFile.exists());

    // Create a session directory that has one file in it.
    File badSes1 = new File(_mmapDir, DbusEventBuffer.getSessionPrefix() + "m");
    badSes1.mkdir();
    badSes1.deleteOnExit();
    File junkFile = new File(badSes1.getAbsolutePath() + "/junkFile");
    junkFile.createNewFile();
    junkFile.deleteOnExit();
    // Create a directory that is empty
    File badSes2 = new File(_mmapDir, DbusEventBuffer.getSessionPrefix() + "n");
    badSes2.mkdir();
    badSes2.deleteOnExit();

    // Create a good file under mmap directory that we don't want to see removed.
    final String goodFile = "GoodFile";
    File gf = new File(_mmapDir, goodFile);
    gf.createNewFile();

    // Now close the multibuf, and see that the new files are still there.
    // We should have deleted the unused sessions and info files.
    bufMult.close();

    HashSet<String> validEntries = new HashSet<String>(bufNum);
    for (DbusEventBuffer dbusBuf : bufMult.bufIterable()) {
        File metaFile = new File(_mmapDir, dbusBuf.metaFileName());
        // check that we don't have the files
        Assert.assertTrue(metaFile.exists());
        validateFiles(metaFile, bufNum);
        validEntries.add(metaFile.getName());
        DbusEventBufferMetaInfo mi = new DbusEventBufferMetaInfo(metaFile);
        mi.loadMetaInfo();
        validEntries.add(mi.getSessionId());
    }

    validEntries.add(goodFile);

    // Now we should be left with meta files, and session dirs and nothing else.
    entries = _mmapDir.listFiles();
    for (File f : entries) {
        Assert.assertTrue(validEntries.contains(f.getName()));
        validEntries.remove(f.getName());
    }
    Assert.assertTrue(validEntries.isEmpty());

    // And everything else should have moved to the .BAK directory
    entries = _mmapBakDir.listFiles();
    HashMap<String, File> fileHashMap = new HashMap<String, File>(entries.length);
    for (File f : entries) {
        fileHashMap.put(f.getName(), f);
    }

    Assert.assertTrue(fileHashMap.containsKey(badSes1.getName()));
    Assert.assertTrue(fileHashMap.get(badSes1.getName()).isDirectory());
    Assert.assertEquals(fileHashMap.get(badSes1.getName()).listFiles().length, 1);
    Assert.assertEquals(fileHashMap.get(badSes1.getName()).listFiles()[0].getName(), junkFile.getName());
    fileHashMap.remove(badSes1.getName());

    Assert.assertTrue(fileHashMap.containsKey(badSes2.getName()));
    Assert.assertTrue(fileHashMap.get(badSes2.getName()).isDirectory());
    Assert.assertEquals(fileHashMap.get(badSes2.getName()).listFiles().length, 0);
    fileHashMap.remove(badSes2.getName());

    // We should have the renamed meta files in the hash now.
    for (File f : entries) {
        if (f.getName().startsWith(DbusEventBuffer.getMmapMetaInfoFileNamePrefix())) {
            Assert.assertTrue(fileHashMap.containsKey(f.getName()));
            Assert.assertTrue(f.isFile());
            fileHashMap.remove(f.getName());
        }
    }

    Assert.assertTrue(fileHashMap.isEmpty());

    // One more test to make sure we create the BAK directory dynamically if it does not exist.
    FileUtils.deleteDirectory(_mmapBakDir);
    bufMult = createBufferMult(config);
    entries = _mmapDir.listFiles();
    // Create an info file for one buffer.
    buf = bufMult.bufIterable().iterator().next();
    buf.saveBufferMetaInfo(true);
    infoFile = new File(_mmapDir, buf.metaFileName() + ".info");
    Assert.assertTrue(infoFile.exists());
    bufMult.close();
    entries = _mmapBakDir.listFiles();
    fileHashMap = new HashMap<String, File>(entries.length);
    for (File f : entries) {
        fileHashMap.put(f.getName(), f);
    }
    Assert.assertTrue(fileHashMap.containsKey(infoFile.getName()));
    Assert.assertTrue(fileHashMap.get(infoFile.getName()).isFile());
}

From source file:dao.PendingfriendDaoDb.java

/**
 * This method adds a pending friend as a friend.
 * @param guestLogin - the guest login that is added as a pending friend
 * @param memberId - the memberId to whom the pending friend is added.
 * @param memberLoginInfo - the members login information bean
 * @throws BaseDaoException//from  w w  w  . j ava  2s  . co m
 **/
public void addPendingFriend(String guestLogin, String memberId, Hdlogin memberLoginInfo)
        throws BaseDaoException {

    /**
      * (loginId = memberId) (sending person) 
      * (destloginId=guestLogin) (Receiving person, receives the request as a friend)
    */
    if (RegexStrUtil.isNull(guestLogin) || RegexStrUtil.isNull(memberId)) {
        throw new BaseDaoException("params are null");
    }

    /**
     * Get the guest's login information from guestLogin
          * the person sending the request, the order is important, do not remove this
     */
    Hdlogin hdlogin = getLoginid(guestLogin);
    if (hdlogin == null) {
        throw new BaseDaoException("hdlogin is null for guestLogin " + guestLogin);
    }
    String guestId = hdlogin.getValue(DbConstants.LOGIN_ID);
    if (RegexStrUtil.isNull(guestId)) {
        throw new BaseDaoException("guestId is null for guestLogin " + guestLogin);
    }
    String gfname = hdlogin.getValue("fname");
    String glname = hdlogin.getValue("lname");
    String to = hdlogin.getValue("email");

    /**
    * if already a friend, ignore it
    */
    List prefResult = null;
    Object[] myParams = { (Object) guestId, (Object) memberId };

    /**
     *  Get scalability datasource for pendingfriends - not partitioned
     */
    String sourceName = scalabilityManager.getWriteZeroScalability();
    ds = scalabilityManager.getSource(sourceName);
    if (ds == null) {
        throw new BaseDaoException("ds null, addPendingfriend() " + sourceName);
    }
    String from = webconstants.getMailfrom();

    /**
    * check if this entry exists for this user, do read_uncommitted also
    */
    HashSet pendingSet = null;
    Connection conn = null;
    try {
        conn = ds.getConnection();
        if (conn != null) {
            conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
            pendingSet = listQuery.run(conn, memberId, guestId);
            conn.close();
        }
    } catch (Exception e) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e1) {
            throw new BaseDaoException("connection close exception ", e1);
        }

        throw new BaseDaoException("error occured in db query", e);
    }

    /**
    * this entry already exists in the pendinglist, return
    */
    if (!pendingSet.isEmpty()) {
        Iterator it1 = pendingSet.iterator();
        Integer count = new Integer(((Pendingfriend) it1.next()).getValue("count(*)"));
        if (count > 0) {
            return;
        }
    }

    /**
     *  add this friend
     */
    try {
        addQuery.run(guestId, memberId);
    } catch (Exception e) {
        throw new BaseDaoException("error occured while adding a PendingfriendAddQuery()" + addQuery.getSql()
                + ", guestId = " + guestId + ", memberId = " + memberId, e);
    }

    /**
     * If the Email "to" field is missing, don't send email, just return
     */
    if (!RegexStrUtil.isNull(to)) {

        /**
         *  Get scalability datasource for hdprofile - partitioned on loginId
         */
        sourceName = scalabilityManager.getReadScalability(guestId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, addPendingfriend() " + sourceName);
        }

        /**
         * check if the guest needs to be notified
         */
        Object[] params = { (Object) guestId };
        //params[0] = guestId;
        prefResult = null;
        try {
            prefResult = myprofileQuery.execute(params);
        } catch (Exception e) {
            throw new BaseDaoException("error occured while executing HdprofileQuery()"
                    + myprofileQuery.getSql() + " guestId = " + params[0], e);
        }

        if (prefResult.size() == 1) {
            Hdprofile hdprofile = (Hdprofile) prefResult.get(0);
            if (hdprofile.getValue("informfd").equalsIgnoreCase("1")) {
                //memberId = memberLoginInfo.getValue(DbConstants.LOGIN_ID);
                String fname = memberLoginInfo.getValue(DbConstants.FIRST_NAME);
                String lname = memberLoginInfo.getValue(DbConstants.LAST_NAME);
                String subject = "Invitation for friendship with " + fname + " " + lname;
                String msg = "Hi, " + gfname + "\n" + fname + " " + lname + " has sent you " + "\n\n"
                        + "Members webpage: " + webconstants.getHddomain() + "/userpage?member="
                        + memberLoginInfo.getValue(DbConstants.LOGIN) + webconstants.getMailfooter();
                hdmail.sendEmail(to, subject, msg, from);
            }
        }
    }

    Fqn fqn = cacheUtil.fqn(DbConstants.USER_PAGE);
    if (treeCache.exists(fqn, guestLogin)) {
        treeCache.remove(fqn, guestLogin);
    }

    fqn = cacheUtil.fqn(DbConstants.USER_PAGE);
    if (treeCache.exists(fqn, memberLoginInfo.getValue(DbConstants.LOGIN))) {
        treeCache.remove(fqn, memberLoginInfo.getValue(DbConstants.LOGIN));
    }
}

From source file:org.apache.ddlutils.io.DatabaseDataIO.java

/**
 * Sorts the given table according to their foreign key order.
 * /*from w ww .jav a2  s .  c  o  m*/
 * @param tables The tables
 * @return The sorted tables
 */
private List sortTables(Table[] tables) {
    ArrayList result = new ArrayList();
    HashSet processed = new HashSet();
    ListOrderedMap pending = new ListOrderedMap();

    for (int idx = 0; idx < tables.length; idx++) {
        Table table = tables[idx];

        if (table.getForeignKeyCount() == 0) {
            result.add(table);
            processed.add(table);
        } else {
            HashSet waitedFor = new HashSet();

            for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) {
                Table waitedForTable = table.getForeignKey(fkIdx).getForeignTable();

                if (!table.equals(waitedForTable)) {
                    waitedFor.add(waitedForTable);
                }
            }
            pending.put(table, waitedFor);
        }
    }

    HashSet newProcessed = new HashSet();

    while (!processed.isEmpty() && !pending.isEmpty()) {
        newProcessed.clear();
        for (Iterator it = pending.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            Table table = (Table) entry.getKey();
            HashSet waitedFor = (HashSet) entry.getValue();

            waitedFor.removeAll(processed);
            if (waitedFor.isEmpty()) {
                it.remove();
                result.add(table);
                newProcessed.add(table);
            }
        }
        processed.clear();

        HashSet tmp = processed;

        processed = newProcessed;
        newProcessed = tmp;
    }
    // the remaining are within circular dependencies
    for (Iterator it = pending.keySet().iterator(); it.hasNext();) {
        result.add(it.next());
    }
    return result;
}

From source file:com.cloud.bridge.io.S3CAStorBucketAdapter.java

private synchronized ScspClient myClient(String mountedRoot) {
    if (_scspClient != null) {
        return _scspClient;
    }//  w w w  .ja va 2s .c  o m
    // The castor cluster is specified either by listing the ip addresses of some nodes, or
    // by specifying "zeroconf=" and the cluster's mdns name -- this is "cluster" in castor's node.cfg.
    // The "domain" to store streams can be specified. If not specified, streams will be written
    // without a "domain" query arg, so they will go into the castor default domain.
    // The port is optional and must be at the end of the config string, defaults to 80.
    // Examples: "castor 172.16.78.130 172.16.78.131 80", "castor 172.16.78.130 domain=mycluster.example.com", 
    // "castor zeroconf=mycluster.example.com domain=mycluster.example.com 80"
    String[] cfg = mountedRoot.split(" ");
    int numIPs = cfg.length - 1;
    String possiblePort = cfg[cfg.length - 1];
    int castorPort = DEFAULT_SCSP_PORT;
    try {
        castorPort = Integer.parseInt(possiblePort);
        --numIPs;
    } catch (NumberFormatException nfe) {
        // okay, it's an ip address, not a port number
    }
    if (numIPs <= 0) {
        throw new ConfigurationException("No CAStor nodes specified in '" + mountedRoot + "'");
    }
    HashSet<String> ips = new HashSet<String>();
    String clusterName = null;
    for (int i = 0; i < numIPs; ++i) {
        String option = cfg[i + 1]; // ip address or zeroconf=mycluster.example.com or domain=mydomain.example.com
        if (option.toLowerCase().startsWith("zeroconf=")) {
            String[] confStr = option.split("=");
            if (confStr.length != 2) {
                throw new ConfigurationException("Could not parse cluster name from '" + option + "'");
            }
            clusterName = confStr[1];
        } else if (option.toLowerCase().startsWith("domain=")) {
            String[] confStr = option.split("=");
            if (confStr.length != 2) {
                throw new ConfigurationException("Could not parse domain name from '" + option + "'");
            }
            _domain = confStr[1];
        } else {
            ips.add(option);
        }
    }
    if (clusterName == null && ips.isEmpty()) {
        throw new ConfigurationException("No CAStor nodes specified in '" + mountedRoot + "'");
    }
    String[] castorNodes = ips.toArray(new String[0]); // list of configured nodes
    if (clusterName == null) {
        try {
            _locator = new StaticLocator(castorNodes, castorPort, LOCATOR_RETRY_TIMEOUT);
            _locator.start();
        } catch (IOException e) {
            throw new ConfigurationException(
                    "Could not create CAStor static locator for '" + Arrays.toString(castorNodes) + "'");
        }
    } else {
        try {
            clusterName = clusterName.replace(".", "_"); // workaround needed for CAStorSDK 1.3.1
            _locator = new ZeroconfLocator(clusterName);
            _locator.start();
        } catch (IOException e) {
            throw new ConfigurationException(
                    "Could not create CAStor zeroconf locator for '" + clusterName + "'");
        }
    }
    try {
        s_logger.info("CAStor client starting: " + (_domain == null ? "default domain" : "domain " + _domain)
                + " " + (clusterName == null ? Arrays.toString(castorNodes) : clusterName) + " :" + castorPort);
        _scspClient = new ScspClient(_locator, castorPort, DEFAULT_MAX_POOL_SIZE, DEFAULT_MAX_RETRIES,
                CONNECTION_TIMEOUT, CM_IDLE_TIMEOUT);
        _scspClient.start();
    } catch (Exception e) {
        s_logger.error("Unable to create CAStor client for '" + mountedRoot + "': " + e.getMessage(), e);
        throw new ConfigurationException("Unable to create CAStor client for '" + mountedRoot + "': " + e);
    }
    return _scspClient;
}