Example usage for java.util TreeSet first

List of usage examples for java.util TreeSet first

Introduction

In this page you can find the example usage for java.util TreeSet first.

Prototype

public E first() 

Source Link

Usage

From source file:de.tudarmstadt.tk.statistics.report.ReportGenerator.java

/**
 * Get a String representation of the models order, inferred from their
 * pairwise p-values./*from  ww  w.j a v a2s. c om*/
 * 
 * @param ordering
 *            a HashMap mapping levels of topological order to sets of
 *            models
 * @return A string representing the models order or alternatively a message
 *         that no order could be determined.
 */
private String getModelOrderingRepresentation(HashMap<Integer, TreeSet<Integer>> ordering) {

    if (ordering != null && ordering.size() > 1) {
        StringBuilder orderSequence = new StringBuilder();
        for (int level = 0; level < ordering.keySet().size(); level++) {
            TreeSet<Integer> s = ordering.get(level);

            if (s.size() == 0)
                return "These results do not allow for a strict ordering of all models.";

            int n = s.first();
            s.remove(n);
            orderSequence.append(String.format("(M%d", n));
            for (Integer node : ordering.get(level)) {
                orderSequence.append(String.format(",M%d", node));
            }
            orderSequence.append(")");

            if (level < ordering.keySet().size() - 1) {
                orderSequence.append("<");
            }
        }
        return String.format("These results allow for the follwing ordering of model performances: %s. ",
                orderSequence.toString());
    } else {
        return "These results do not allow for a strict ordering of all models. ";
    }
}

From source file:spectrogram.Spectrogram.java

/**
 * Using previously set up object members verify the channel and get needed info, complication is
 * when the channel is only partially specified
 *
 * @return true if we got what we needed, else return after we've printed errors.
 *///from  ww  w  .j a  v  a  2  s .c o m
private boolean getChanInfo() throws SQLException {
    boolean ret = false;
    long strt = System.currentTimeMillis();
    {
        int n;
        if (channelName == null || channelName.isEmpty()) {
            throw new IllegalArgumentException("No Channel specified");
        }
        if ((server == null || server.isEmpty()) && (cType == null || cType.isEmpty())) {
            n = chanTbl.getBestMatch(channelName);
            chanInfo = chanTbl.getChanInfo(n);
            server = chanInfo.getServer();
        } else {
            if (cType == null || cType.isEmpty()) {
                cType = "raw";
            }
            TreeSet<ChanInfo> chSet = chanTbl.getAsSet(server, channelName, cType, 10);
            if (chSet.size() > 1) {
                chanInfo = null;
                if (sampleRate > 0) {
                    for (ChanInfo ci : chSet) {
                        if (Math.abs(ci.getRate() - sampleRate) < .0001) {
                            chanInfo = ci;
                            break;
                        }
                    }
                }
                if (chanInfo == null) {
                    System.err.print("Warning: more than one channel matches: " + channelName);
                    System.err.println(" and no applicable rate specified.");
                }
            } else if (chSet.size() == 1) {
                chanInfo = chSet.first();
            }
        }
        if (chanInfo == null) {
            System.err.println("Channel requested was not found: " + channelName);
        } else {
            sampleRate = chanInfo.getRate();
            String dtyp = chanInfo.getdType();

            if (dtyp.equalsIgnoreCase("INT-16")) {
                bytesPerSample = 2;
            } else if (dtyp.equalsIgnoreCase("INT-32")) {
                bytesPerSample = 4;
            } else if (dtyp.equalsIgnoreCase("INT-64")) {
                bytesPerSample = 8;
            } else if (dtyp.equalsIgnoreCase("FLT-32")) {
                bytesPerSample = 4;
            } else if (dtyp.equalsIgnoreCase("FLT-64")) {
                bytesPerSample = 8;
            } else if (dtyp.equalsIgnoreCase("CPX-64")) {
                bytesPerSample = 8;
            }
            ret = true;

            float dur = (System.currentTimeMillis() - strt) / 1000.f;
            System.out.format("Get channel info took %1$.1f sec.\n", dur);
        }
    }
    return ret;
}

From source file:org.unitime.timetable.action.PersonalizedExamReportAction.java

protected String getMeetingTime(Class_ clazz) {
    String meetingTime = "";
    Formats.Format<Date> dpf = Formats.getDateFormat(Formats.Pattern.DATE_EVENT_SHORT);
    Assignment assignment = clazz.getCommittedAssignment();
    TreeSet meetings = (clazz.getEvent() == null ? null : new TreeSet(clazz.getEvent().getMeetings()));
    DatePattern dp = (assignment == null ? null : assignment.getDatePattern());
    if (meetings != null && !meetings.isEmpty()) {
        int dayCode = getDaysCode(meetings);
        String days = "";
        for (int i = 0; i < Constants.DAY_CODES.length; i++)
            if ((dayCode & Constants.DAY_CODES[i]) != 0)
                days += CONSTANTS.shortDays()[i];
        meetingTime += days;/*from   www.ja  v a2  s. c o m*/
        Meeting first = (Meeting) meetings.first();
        meetingTime += " " + first.startTime() + " - " + first.stopTime();
    } else if (assignment != null) {
        TimeLocation t = assignment.getTimeLocation();
        meetingTime += t.getDayHeader() + " " + t.getStartTimeHeader(CONSTANTS.useAmPm()) + " - "
                + t.getEndTimeHeader(CONSTANTS.useAmPm());
    } else {
        meetingTime += "Arr Hrs";
    }
    if (meetings != null && !meetings.isEmpty()) {
        if (dp == null || !dp.isDefault()) {
            Date first = ((Meeting) meetings.first()).getMeetingDate();
            Date last = ((Meeting) meetings.last()).getMeetingDate();
            if (dp != null && dp.getType() == DatePattern.sTypeAlternate)
                meetingTime += " (" + dpf.format(first) + " - " + dpf.format(last) + " " + dp.getName() + ")";
            else
                meetingTime += " (" + dpf.format(first) + " - " + dpf.format(last) + ")";
        }
    } else if (dp != null && !dp.isDefault()) {
        if (dp.getType() == DatePattern.sTypeAlternate)
            meetingTime += " (" + dpf.format(dp.getStartDate()) + " - " + dpf.format(dp.getEndDate()) + " "
                    + dp.getName() + ")";
        else
            meetingTime += " (" + dpf.format(dp.getStartDate()) + " - " + dpf.format(dp.getEndDate()) + ")";
    }
    return meetingTime;
}

From source file:org.cloudata.core.tabletserver.DiskSSTable.java

public ColumnValue findClosestMeta(Row.Key rowKey, String columnName, boolean great) throws IOException {
    lock.obtainReadLock();/*from w  w  w .j a  v  a  2 s. c  o  m*/
    try {
        if (columnMemoryCaches.containsKey(columnName)) {
            ColumnMemoryCache cache = columnMemoryCaches.get(columnName);
            return cache.findClosest(rowKey);
        }
        List<TabletMapFile> tabletMapFiles = mapFiles.get(columnName);
        if (tabletMapFiles == null || tabletMapFiles.isEmpty()) {
            return null;
        }

        MapFileReader[] readers = new MapFileReader[tabletMapFiles.size()];

        TreeSet<MetaValue> metaValues = new TreeSet<MetaValue>();

        TreeSet<ColumnValue> workPlace = new TreeSet<ColumnValue>();

        try {
            //init
            CellFilter cellFilter = new CellFilter(columnName);
            int index = 0;
            for (TabletMapFile tabletMapFile : tabletMapFiles) {
                MapFileReader reader = tabletMapFile.getMapFileReader(rowKey, Row.Key.MAX_KEY, cellFilter);
                ColumnValue columnValue = null;
                while ((columnValue = reader.next()) != null) {
                    if (great) {
                        if (columnValue.getRowKey().compareTo(rowKey) < 0) {
                            continue;
                        }
                    } else {
                        if (columnValue.getRowKey().compareTo(rowKey) <= 0) {
                            continue;
                        }
                    }
                    break;
                }
                if (columnValue != null) {
                    workPlace.add(columnValue);
                    readers[index] = reader;
                } else {
                    reader.close();
                    readers[index] = null;
                }
                index++;
            }

            //findClosestMeta
            while (true) {
                if (workPlace.isEmpty()) {
                    return null;
                }
                ColumnValue winnerColumnValue = workPlace.first();
                metaValues.add(new MetaValue(winnerColumnValue));
                workPlace.remove(winnerColumnValue);
                Row.Key winnerRowKey = winnerColumnValue.getRowKey();

                List<ColumnValue> tempWorkPlace = new ArrayList<ColumnValue>();
                tempWorkPlace.addAll(workPlace);
                for (ColumnValue eachColumnValue : tempWorkPlace) {
                    if (winnerRowKey.equals(eachColumnValue.getRowKey())) {
                        metaValues.add(new MetaValue(eachColumnValue));
                        workPlace.remove(eachColumnValue);
                    }
                }

                for (int i = 0; i < readers.length; i++) {
                    if (readers[i] == null) {
                        continue;
                    }
                    ColumnValue columnValue = null;
                    while ((columnValue = readers[i].next()) != null) {
                        if (winnerRowKey.equals(columnValue.getRowKey())) {

                            metaValues.add(new MetaValue(columnValue));
                        } else {
                            workPlace.add(columnValue);
                            break;
                        }
                    }
                    if (columnValue == null) {
                        readers[i].close();
                        readers[i] = null;
                    }
                }

                if (metaValues.size() > 0) {
                    MetaValue firstValue = metaValues.first();
                    if (!firstValue.columnValue.isDeleted()) {
                        return firstValue.columnValue;
                    } else {
                        metaValues.clear();
                    }
                }
            }
        } finally {
            for (int i = 0; i < readers.length; i++) {
                try {
                    if (readers[i] != null) {
                        readers[i].close();
                    }
                } catch (Exception e) {
                    LOG.warn("Can't close MapFileReader:" + e.getMessage());
                }
            }
        }
    } finally {
        lock.releaseReadLock();
    }
}

From source file:edu.duke.cabig.c3pr.domain.StudySubject.java

@Transient
public StudyVersion getFirstConsentedStudyVersion() {

    TreeSet uniqueStudyVersions = new TreeSet();
    List<StudySubjectConsentVersion> allStudySubjectConsentVersions = getAllConsents();
    for (StudySubjectConsentVersion studySubjectConsentVersion : allStudySubjectConsentVersions) {
        uniqueStudyVersions.add(studySubjectConsentVersion.getConsent().getStudyVersion());
    }//from  www . ja v a  2  s. c  o m

    return uniqueStudyVersions.isEmpty() ? null : (StudyVersion) uniqueStudyVersions.first();
}

From source file:org.sourceopen.hadoop.hbase.replication.consumer.FileChannelRunnable.java

/**
 * ?zk?group?currentNode/*from ww w  .j  a va2 s .  c  om*/
 * 
 * @return
 */
protected ConsumerZNode tryToMakeACurrentNode() {
    // ??
    if (CollectionUtils.isNotEmpty(errorNodeList)) {
        ConsumerZNode orig = null;
        String retry = null;
        for (ConsumerZNode node : errorNodeList) {
            String groupRoot = root.getPath() + ConsumerConstants.FILE_SEPERATOR + node.getGroupName();
            String queue = groupRoot + ConsumerConstants.FILE_SEPERATOR + ConsumerConstants.ZK_QUEUE;
            try {
                TreeSet<String> ftsSet = getQueueData(queue);
                for (String fileName : ftsSet) {
                    if (isRetry(fileName, node.getFileName())) {
                        orig = node;
                        retry = fileName;
                        break;
                    }
                }
                if (retry != null) {
                    break;
                }
            } catch (ClassNotFoundException e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error("queue ClassNotFoundException. queue:" + queue, e);
                }
            } catch (Exception e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("Can't get queue from zk. queue:" + queue, e);
                }
            }
        }
        if (retry != null) {
            errorNodeList.remove(orig);
            orig.setFileName(retry);
            return orig;
        }
    }
    // ???
    List<String> groupList = null;
    try {
        groupList = zoo.getChildren(root.getPath(), false);
    } catch (Exception e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("no group found at zk. " + root.getPath(), e);
        }
    }
    if (CollectionUtils.isNotEmpty(groupList)) {
        for (String group : groupList) {
            String groupRoot = root.getPath() + ConsumerConstants.FILE_SEPERATOR + group;
            String cur = groupRoot + ConsumerConstants.FILE_SEPERATOR + ConsumerConstants.ZK_CURRENT;
            String queue = groupRoot + ConsumerConstants.FILE_SEPERATOR + ConsumerConstants.ZK_QUEUE;
            Stat statZkCur;
            try {
                statZkCur = zoo.exists(cur, false);
            } catch (Exception e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("zk connection error while trying znode of cur: " + cur, e);
                }
                continue;
            }
            if (statZkCur == null) {
                // group?
                try {
                    zoo.create(cur, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
                } catch (NodeExistsException e) {
                    continue;
                } catch (Exception e) {
                    LOG.error("lock group error", e);
                }
                try {
                    statZkCur = zoo.exists(cur, false);
                    TreeSet<String> ftsSet = getQueueData(queue);
                    if (CollectionUtils.isEmpty(ftsSet)) {
                        continue;
                    }
                    String fileName = ftsSet.first();
                    ProtocolHead fileHead = HDFSFileAdapter.validataFileName(fileName);
                    if (fileHead == null && LOG.isErrorEnabled()) {
                        throw new RuntimeException("validataFileName fail. fileName: " + fileName);
                    }
                    ConsumerZNode tmpNode = new ConsumerZNode();
                    tmpNode.setFileLastModified(fileHead.getFileTimestamp());
                    tmpNode.setFileName(fileName);
                    tmpNode.setGroupName(group);
                    tmpNode.setProcessorName(this.name);
                    tmpNode.setStatusLastModified(System.currentTimeMillis());
                    tmpNode.setVersion(statZkCur.getVersion());
                    statZkCur = zoo.setData(cur, tmpNode.toByteArray(), statZkCur.getVersion());
                    tmpNode.setVersion(statZkCur.getVersion());
                    return tmpNode;
                } catch (Exception e) {
                    if (LOG.isWarnEnabled()) {
                        LOG.warn("error while creating znode of cur: " + cur, e);
                    }
                    try {
                        zoo.delete(cur, ConsumerConstants.ZK_ANY_VERSION);
                    } catch (Exception e1) {
                        if (LOG.isErrorEnabled()) {
                            LOG.error("error while deleting znode of cur: " + cur, e);
                        }
                    }
                    continue;
                }
            }
        }
    }
    return null;
}

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

/**
 * add an element to the XForms document: the bind + the control
 * (only the control if "withBind" is false)
 *//*from  w  w  w.ja v a2  s  . c o m*/
private Element addElement(final Document xformsDocument, final Element modelSection,
        final Element defaultInstanceElement, final Element formSection, final XSModel schema,
        final XSElementDeclaration elementDecl, final String pathToRoot, final SchemaUtil.Occurrence occurs,
        final ResourceBundle resourceBundle) throws FormBuilderException {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("[addElement] adding element " + elementDecl + " at path " + pathToRoot);

    XSTypeDefinition controlType = elementDecl.getTypeDefinition();
    if (controlType == null) {
        // TODO!!! Figure out why this happens... for now just warn...
        // seems to happen when there is an element of type IDREFS
        LOGGER.warn("WARNING!!! controlType is null for " + elementDecl + ", " + elementDecl.getName());
        return null;
    }

    if (controlType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE
            && ((XSComplexTypeDefinition) controlType)
                    .getContentType() == XSComplexTypeDefinition.CONTENTTYPE_SIMPLE) {
        controlType = ((XSComplexTypeDefinition) controlType).getSimpleType();
    }

    if (controlType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {
        return this.addSimpleType(xformsDocument, modelSection, formSection, schema,
                (XSSimpleTypeDefinition) controlType, elementDecl.getName(), elementDecl, pathToRoot, occurs,
                resourceBundle);
    }

    if (controlType.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE
            && "anyType".equals(controlType.getName())) {
        return this.addSimpleType(xformsDocument, modelSection, formSection, schema,
                (XSComplexTypeDefinition) controlType, elementDecl.getName(), elementDecl, pathToRoot, occurs,
                resourceBundle);
    }

    if (controlType.getTypeCategory() != XSTypeDefinition.COMPLEX_TYPE) {
        throw new FormBuilderException(
                "Unsupported type [" + elementDecl.getType() + "] for node [" + controlType.getName() + "]");
    }

    boolean relative = true;
    final String typeName = controlType.getName();
    final boolean typeIsAbstract = ((XSComplexTypeDefinition) controlType).getAbstract();
    final TreeSet<XSTypeDefinition> compatibleTypes = typeName != null ? this.typeTree.get(typeName) : null;

    if (compatibleTypes == null && typeName != null && LOGGER.isDebugEnabled()) {
        LOGGER.debug("[addElement] No compatible type found for " + typeName);
    }

    if (typeName != null && compatibleTypes != null) {
        relative = false;

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("[addElement] compatible types for " + typeName + ":");
            for (XSTypeDefinition compType : compatibleTypes) {
                LOGGER.debug("[addElement]   compatible type name = " + compType.getName());
            }
        }

        if (!typeIsAbstract && compatibleTypes.size() == 0) {
            relative = true;
        } else if (typeIsAbstract && compatibleTypes.size() == 1) {
            // only one compatible type, set the controlType value
            // and fall through
            controlType = compatibleTypes.first();
            relative = true;
        } else if (typeIsAbstract && compatibleTypes.size() == 0) {
            // name not null but no compatibleType?
            relative = false;
        } else {
            return this.addElementWithMultipleCompatibleTypes(xformsDocument, modelSection,
                    defaultInstanceElement, formSection, schema, elementDecl, compatibleTypes, pathToRoot,
                    resourceBundle, occurs);
        }
    }

    if (!relative) {
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("[addElement] bind is not relative for " + elementDecl.getName());
    } else {
        //         final SchemaUtil.Occurrence occurs = SchemaUtil.getOccurrence(elementDecl);
        //create the bind in case it is a repeat
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("[addElement] Adding empty bind for control " + controlType + " type " + typeName
                    + " nodeset " + pathToRoot + " occurs " + occurs);
        }

        // create the <xforms:bind> element and add it to the model.
        boolean isRepeated = isRepeated(occurs, controlType);
        final Element bindElement = this.createBind(xformsDocument,
                pathToRoot + (isRepeated ? "[position() != last()]" : ""));

        modelSection.appendChild(bindElement);
        this.startBindElement(bindElement, schema, controlType, null, occurs);
    }
    return this.addComplexType(xformsDocument, modelSection, defaultInstanceElement, formSection, schema,
            (XSComplexTypeDefinition) controlType, elementDecl, pathToRoot, occurs, true, false,
            resourceBundle);
}

From source file:org.prom5.analysis.performance.PerformanceAnalysisGUI.java

/**
 * Initializes the waiting time levels. If no manual peformance settings are
 * filled in by the user, standard settings are calculated and used.
 * Standard settings: approximately 33% low, 33% high, 33% medium level
 * note that a set is used instead of a list however, so if a time occurs
 * multiple times (this can happen easily with a waiting time of 0.0s for
 * instance) of such places only one is used, so the 33-33-33 estimation can
 * be quite wrong, though this is not considered to be a problem.
 *//*from   w w w  .j a  v  a  2 s .  c o m*/
public void initializeWaitingTimeLevels() {
    if (!manualSettings) {
        //no manual settings are present
        TreeSet waitingTimes = new TreeSet();
        ListIterator it = extendedPetriNet.getPlaces().listIterator();
        while (it.hasNext()) {
            //place the mean waiting time of each place in the tree set
            ExtendedPlace p = (ExtendedPlace) it.next();
            p.calculateMetrics(extendedLog.getLogTraceIDs(), advancedSettings[1], failedInstances);
            if (p.getMeanWaitingTime() >= 0) {
                //only add correct times
                double waitTime = p.getMeanWaitingTime() / timeDivider;
                waitingTimes.add(Double.valueOf(waitTime));
            }
        }
        int num = waitingTimes.size() / 3;
        //remove the first 'num' measurements and the last 'num' measurements
        //from waitingTimes
        for (int i = 0; i < num; i++) {
            //there should be at least one waiting time measurement remaining
            if (!(waitingTimes.size() < 2)) {
                waitingTimes.remove(waitingTimes.first());
                waitingTimes.remove(waitingTimes.last());
            }
        }

        //give new values to the bounds and the colors
        if (waitingTimes.size() != 0) {
            Double bnd = (Double) waitingTimes.first();
            bounds.set(0, bnd);
            bnd = (Double) waitingTimes.last();
            bounds.set(1, bnd);
        } else {
            //in case there are no valid waiting times
            waitingTimes.add(Double.valueOf(0));
            Double bnd = (Double) waitingTimes.first();
            bounds.set(0, bnd);
            bounds.set(1, bnd);
        }
        levelColors.set(0, Color.BLUE);
        levelColors.set(1, Color.YELLOW);
        levelColors.set(2, Color.MAGENTA);
    }
    extendedPetriNet.setAdvancedSettings(advancedSettings);
    extendedPetriNet.setBounds(bounds);
    extendedPetriNet.setLevelColors(levelColors);
    extendedPetriNet.setTimeDivider(timeDivider);
    extendedPetriNet.setFailedInstances(failedInstances);
}

From source file:org.processmining.analysis.performance.PerformanceAnalysisGUI.java

/**
 * Initializes the waiting time levels. If no manual peformance settings are
 * filled in by the user, standard settings are calculated and used.
 * Standard settings: approximately 33% low, 33% high, 33% medium level note
 * that a set is used instead of a list however, so if a time occurs
 * multiple times (this can happen easily with a waiting time of 0.0s for
 * instance) of such places only one is used, so the 33-33-33 estimation can
 * be quite wrong, though this is not considered to be a problem.
 *//*from   ww  w  .  ja  va  2 s . c  o m*/
public void initializeWaitingTimeLevels() {
    if (!manualSettings) {
        // no manual settings are present
        TreeSet waitingTimes = new TreeSet();
        ListIterator it = extendedPetriNet.getPlaces().listIterator();
        while (it.hasNext()) {
            // place the mean waiting time of each place in the tree set
            ExtendedPlace p = (ExtendedPlace) it.next();
            p.calculateMetrics(extendedLog.getLogTraceIDs(), advancedSettings[1], failedInstances);
            if (p.getMeanWaitingTime() >= 0) {
                // only add correct times
                double waitTime = p.getMeanWaitingTime() / timeDivider;
                waitingTimes.add(Double.valueOf(waitTime));
            }
        }
        int num = waitingTimes.size() / 3;
        // remove the first 'num' measurements and the last 'num'
        // measurements
        // from waitingTimes
        for (int i = 0; i < num; i++) {
            // there should be at least one waiting time measurement
            // remaining
            if (!(waitingTimes.size() < 2)) {
                waitingTimes.remove(waitingTimes.first());
                waitingTimes.remove(waitingTimes.last());
            }
        }

        // give new values to the bounds and the colors
        if (waitingTimes.size() != 0) {
            Double bnd = (Double) waitingTimes.first();
            bounds.set(0, bnd);
            bnd = (Double) waitingTimes.last();
            bounds.set(1, bnd);
        } else {
            // in case there are no valid waiting times
            waitingTimes.add(Double.valueOf(0));
            Double bnd = (Double) waitingTimes.first();
            bounds.set(0, bnd);
            bounds.set(1, bnd);
        }
        levelColors.set(0, Color.BLUE);
        levelColors.set(1, Color.YELLOW);
        levelColors.set(2, Color.MAGENTA);
    }
    extendedPetriNet.setAdvancedSettings(advancedSettings);
    extendedPetriNet.setBounds(bounds);
    extendedPetriNet.setLevelColors(levelColors);
    extendedPetriNet.setTimeDivider(timeDivider);
    extendedPetriNet.setFailedInstances(failedInstances);
}

From source file:com.cloudera.recordbreaker.schemadict.SchemaSuggest.java

/**
 * This method infers new schema labels for each element in the input.  It returns a Schema object that
 * has the identical format as the input file's Schema object, but the labels may be changed.
 *//*  www .j av  a2s . c om*/
public List<DictionaryMapping> inferSchemaMapping(File avroFile, int k) throws IOException {
    SchemaStatisticalSummary srcSummary = new SchemaStatisticalSummary("input");
    Schema srcSchema = srcSummary.createSummaryFromData(avroFile);
    srcSummary.setUseAttributeLabels(useAttributeLabels);

    //
    // Compare the statistics to the database of schema statistics.  Find the closest matches, both
    // on a per-attribute basis and structurally.
    //
    int schemaSize = srcSchema.getFields().size();
    //
    // We start testing the input database against known schemas that have an identical
    // number of attributes, which should allow for the best matches.  This gives us an
    // initial set of distances.  We then expand the search to schemas of greater or fewer
    // attributes, as long as a given bucket of size-k schemas has a min-distance of less
    // than the current top-k matches.
    //
    //
    TreeSet<DictionaryMapping> sorter = new TreeSet<DictionaryMapping>();
    int numMatches = 0;
    List<Integer> seenIndexes = new ArrayList<Integer>();
    int searchRadius = 0;
    boolean seenAllCandidates = false;
    int srcSchemaSize = srcSchema.getFields().size();
    int totalSchemasExamined = 0;

    while (!seenAllCandidates) {
        // Examine the relevant schema buckets, compute all matches to those schemas
        for (int j = Math.max(1, srcSchemaSize - searchRadius); j <= Math.min(NUM_BUCKETS,
                srcSchemaSize + searchRadius); j++) {

            if (seenIndexes.contains(j - 1)) {
                continue;
            }
            for (SchemaDictionaryEntry elt : dictBySize.get(j - 1)) {
                /////////////////////////////
                // This is where we instrument the mapping stuff.
                // If the pair is an interesting one, then emit the mapping that
                // we discover.  Why are good matches going undiscovered?
                /////////////////////////////
                SchemaMapping mapping = srcSummary.getBestMapping(elt.getSummary());
                if (srcSchema.getName().equals(elt.getSchema().getName())) {
                    System.err
                            .println("Comparing " + srcSchema.getName() + " with " + elt.getSchema().getName());
                    System.err.println("Obtained mapping: " + mapping.toString());
                }

                totalSchemasExamined++;
                sorter.add(new DictionaryMapping(mapping, elt));
                numMatches++;
            }
            seenIndexes.add(j - 1);
        }

        // Have we examined the entire corpus of known schemas?
        if ((srcSchemaSize - searchRadius) <= 1 && (srcSchemaSize + searchRadius) >= NUM_BUCKETS) {
            seenAllCandidates = true;
        } else {
            // Test to see if the best matches are good enough that we can stop looking.
            // We compare the lowest known match distance to the minimum distance for matches
            // in the closest non-examined buckets.
            int lowestSize = srcSchemaSize - searchRadius - 1;
            int highestSize = srcSchemaSize + searchRadius + 1;
            double minNearbyDistance = Double.MAX_VALUE;
            if (lowestSize >= 1) {
                minNearbyDistance = Math.min(minNearbyDistance,
                        SchemaStatisticalSummary.getMinimumMappingCost(srcSchemaSize, lowestSize));
            }
            if (highestSize <= NUM_BUCKETS) {
                minNearbyDistance = Math.min(minNearbyDistance,
                        SchemaStatisticalSummary.getMinimumMappingCost(srcSchemaSize, highestSize));
            }
            // Grab from the Sorter the elt that is MIN_ELTS_SUGGESTED into the sorted list
            if (sorter.size() >= k) {
                DictionaryMapping testDictMapping = null;
                int idx = 0;
                for (DictionaryMapping cur : sorter) {
                    idx++;
                    if (idx == k) {
                        testDictMapping = cur;
                        break;
                    }
                }
                if (testDictMapping.getMapping().getDist() < minNearbyDistance) {
                    seenAllCandidates = true;
                }
            }
        }
        searchRadius++;
    }

    // Return the k best schema mappings
    double smallestDistance = sorter.first().getMapping().getDist();
    List<DictionaryMapping> dsts = new ArrayList<DictionaryMapping>();
    for (DictionaryMapping dp : sorter) {
        if (dsts.size() > k && dp.getMapping().getDist() > smallestDistance) {
            break;
        }
        dsts.add(dp);
    }
    double pct = totalSchemasExamined / (1.0 * dict.contents().size());
    System.err.println("Final search radius of " + searchRadius + " yielded a search over " + pct
            + " of all known databases.");
    return dsts;
}