public static Map encodeGeneralized()

in duplicates/ide/impl/src/org/netbeans/modules/jackpot30/impl/duplicates/ComputeDuplicates.java [334:401]


    public static Map<String, long[]> encodeGeneralized(final Trees trees, final CompilationUnitTree cut) {
        final SourcePositions sp = trees.getSourcePositions();
        final Map<String, Collection<Long>> positions = new HashMap<String, Collection<Long>>();

        new TreePathScanner<Void, Void>() {
            @Override
            public Void scan(Tree tree, Void p) {
                if (tree == null) return null;
                if (getCurrentPath() != null) {
                    DigestOutputStream baos = null;
                    PrintWriter out = null;
                    try {
                        baos = new DigestOutputStream(new ByteArrayOutputStream(), MessageDigest.getInstance("MD5"));
                        out = new PrintWriter(new OutputStreamWriter(baos, "UTF-8"));
                        GeneralizePattern gen = new GeneralizePattern(out, trees);
                        gen.scan(new TreePath(getCurrentPath(), tree), null);
                        out.close();
                        if (gen.value >= MINIMAL_VALUE) {
                            StringBuilder text = new StringBuilder();
                            byte[] bytes = baos.getMessageDigest().digest();
                            for (int cntr = 0; cntr < 4; cntr++) {
                                text.append(String.format("%02X", bytes[cntr]));
                            }
                            text.append(':').append(gen.value);
                            String enc = text.toString();
                            Collection<Long> spanSpecs = positions.get(enc);
                            if (spanSpecs == null) {
                                positions.put(enc, spanSpecs = new LinkedList<Long>());
//                            } else {
//                                spanSpecs.append(";");
                            }
                            long start = sp.getStartPosition(cut, tree);
//                            spanSpecs.append(start).append(":").append(sp.getEndPosition(cut, tree) - start);
                            spanSpecs.add(start);
                            spanSpecs.add(sp.getEndPosition(cut, tree));
                        }
                    } catch (UnsupportedEncodingException ex) {
                        Exceptions.printStackTrace(ex);
                    } catch (NoSuchAlgorithmException ex) {
                        Exceptions.printStackTrace(ex);
                    } finally {
                        try {
                            baos.close();
                        } catch (IOException ex) {
                            Exceptions.printStackTrace(ex);
                        }
                        out.close();
                    }
                }
                return super.scan(tree, p);
            }
        }.scan(cut, null);

        Map<String, long[]> result = new TreeMap<String, long[]>();

        for (Entry<String, Collection<Long>> e : positions.entrySet()) {
            long[] spans = new long[e.getValue().size()];
            int idx = 0;

            for (Long l : e.getValue()) {
                spans[idx++] = l;
            }

            result.put(e.getKey(), spans);
        }

        return result;
    }