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

import static com.shaie.utils.Utils.*;

import java.io.IOException;
import java.util.List;

import org.apache.lucene.index.LeafReader;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.util.BytesRef;

import com.google.common.collect.Lists;

public abstract class IndexUtils {

    private IndexUtils() {
        // No instances should be created.
    }

    /** Prints the terms indexed under the given fields. */
    public static void printFieldTerms(LeafReader reader, String... fields) throws IOException {
        for (final String field : fields) {
            System.out.println(format("Terms for field [%s]:", field));
            final TermsEnum te = reader.terms(field).iterator();
            BytesRef scratch;
            while ((scratch = te.next()) != null) {
                System.out.println(format("  %s", scratch.utf8ToString()));
            }
        }
    }

    /** Prints the terms indexed under the given fields with full postings information. */
    public static void printFieldTermsWithInfo(LeafReader reader, String... fields) throws IOException {
        for (final String field : fields) {
            System.out.println(format("Terms for field [%s], with positional info:", field));
            final TermsEnum te = reader.terms(field).iterator();
            BytesRef scratch;
            PostingsEnum postings = null;
            while ((scratch = te.next()) != null) {
                System.out.println(format("  %s", scratch.utf8ToString()));
                postings = te.postings(postings, PostingsEnum.ALL);
                for (postings.nextDoc(); postings.docID() != DocIdSetIterator.NO_MORE_DOCS; postings.nextDoc()) {
                    final List<Integer> positions = Lists.newArrayList();
                    for (int i = 0; i < postings.freq(); i++) {
                        positions.add(postings.nextPosition());
                    }
                    System.out.println(
                            format("    doc=%d, freq=%d, pos=%s", postings.docID(), postings.freq(), positions));
                }
            }
        }
    }

}

@@ -906,20 +906,47 @@
 va.util.
-List
+Map;%0Aimport java.util.Map.Entry
 ;%0A%0Aimpor
@@ -1194,20 +1194,19 @@
 collect.
-List
+Map
 s;%0A%0Apubl
@@ -2652,20 +2652,19 @@
 nal 
-List
+Map
 %3CInteger
 %3E po
@@ -2659,16 +2659,26 @@
 %3CInteger
+, BytesRef
 %3E positi
@@ -2687,30 +2687,77 @@
 s = 
-List
+Map
 s.new
-ArrayList
+TreeMap
 ();%0A
+                    boolean addedPayload = false;%0A
     
@@ -2844,70 +2844,685 @@
     
-positions.add(postings.nextPosition());%0A                    %7D%0A
+final int pos = postings.nextPosition();%0A                        final BytesRef payload = postings.getPayload();%0A                        if (payload != null) %7B%0A                            positions.put(pos, BytesRef.deepCopyOf(payload));%0A                            addedPayload = true;%0A                        %7D else %7B%0A                            positions.put(pos, null);%0A                        %7D%0A                    %7D%0A                    if (addedPayload) %7B%0A                        System.out.println(format(%22    doc=%25d, freq=%25d%22, postings.docID(), postings.freq(), positions));%0A                        for (final Entry%3CInteger, BytesRef%3E e : positions.entrySet()) %7B%0A      
     
@@ -3529,32 +3529,34 @@
                 
+  
 System.out.print
@@ -3558,16 +3558,78 @@
 println(
+format(%22      pos=%25d, payload=%25s%22, e.getKey(), e.getValue()));
 %0A       
@@ -3641,24 +3641,30 @@
         
+    %7D%0A
         
 format(%22
@@ -3655,16 +3655,80 @@
         
+            %7D else %7B%0A                        System.out.println(
 format(%22
@@ -3787,37 +3787,100 @@
 ings.freq(),
- positions));
+%0A                                positions.keySet()));%0A                    %7D
 %0A           

Print payload information of positions