package org.apache.lucene.search;

/*
 * 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.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/** Expert: Describes the score computation for document and query. */
public class Explanation {

  /**
   * Create a new explanation for a match.
   * @param value       the contribution to the score of the document
   * @param description how {@code value} was computed
   * @param details     sub explanations that contributed to this explanation
   */
  public static Explanation match(float value, String description, Collection<Explanation> details) {
    return new Explanation(true, value, description, details);
  }

  /**
   * Create a new explanation for a match.
   * @param value       the contribution to the score of the document
   * @param description how {@code value} was computed
   * @param details     sub explanations that contributed to this explanation
   */
  public static Explanation match(float value, String description, Explanation... details) {
    return new Explanation(true, value, description, Arrays.asList(details));
  }

  /**
   * Create a new explanation for a document which does not match.
   */
  public static Explanation noMatch(String description, Collection<Explanation> details) {
    return new Explanation(false, 0f, description, Collections.<Explanation>emptyList());
  }

  /**
   * Create a new explanation for a document which does not match.
   */
  public static Explanation noMatch(String description, Explanation... details) {
    return new Explanation(false, 0f, description, Collections.<Explanation>emptyList());
  }

  private final boolean match;                          // whether the document matched
  private final float value;                            // the value of this node
  private final String description;                     // what it represents
  private final List<Explanation> details;              // sub-explanations

  /** Create a new explanation  */
  private Explanation(boolean match, float value, String description, Collection<Explanation> details) {
    this.match = match;
    this.value = value;
    this.description = Objects.requireNonNull(description);
    this.details = Collections.unmodifiableList(new ArrayList<>(details));
    for (Explanation detail : details) {
      Objects.requireNonNull(detail);
    }
  }

  /**
   * Indicates whether or not this Explanation models a match.
   */
  public boolean isMatch() {
    return match;
  }
  
  /** The value assigned to this explanation node. */
  public float getValue() { return value; }

  /** A description of this explanation node. */
  public String getDescription() { return description; }

  /**
   * A short one line summary which should contain all high level
   * information about this Explanation, without the "Details"
   */
  protected String getSummary() {
    return getValue() + " = " + getDescription();
  }
  
  /** The sub-nodes of this explanation node. */
  public Explanation[] getDetails() {
    return details.toArray(new Explanation[0]);
  }

  /** Render an explanation as text. */
  @Override
  public String toString() {
    return toString(0);
  }
  protected String toString(int depth) {
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < depth; i++) {
      buffer.append("  ");
    }
    buffer.append(getSummary());
    buffer.append("\n");

    Explanation[] details = getDetails();
    for (int i = 0 ; i < details.length; i++) {
      buffer.append(details[i].toString(depth+1));
    }

    return buffer.toString();
  }


  /** Render an explanation as HTML. */
  public String toHtml() {
    StringBuilder buffer = new StringBuilder();
    buffer.append("<ul>\n");

    buffer.append("<li>");
    buffer.append(getSummary());
    buffer.append("<br />\n");

    Explanation[] details = getDetails();
    for (int i = 0 ; i < details.length; i++) {
      buffer.append(details[i].toHtml());
    }

    buffer.append("</li>\n");
    buffer.append("</ul>\n");

    return buffer.toString();
  }
}

@@ -1071,16 +1071,22 @@
 %0Apublic 
+final 
 class Ex
@@ -1097,16 +1097,16 @@
 ation %7B%0A
-
 %0A  /**%0A 
@@ -2185,44 +2185,15 @@
 on, 
-Collections.%3CExplanation%3EemptyList()
+details
 );%0A 
@@ -2412,43 +2412,29 @@
 on, 
-Collections.%3CExplanation%3EemptyList(
+Arrays.asList(details
 ));%0A
@@ -3517,158 +3517,15 @@
 %0A%0A  
-/**%0A   * A short one line summary which should contain all high level%0A   * information about this Explanation, without the %22Details%22%0A   */%0A  protected
+private
  Str
@@ -3853,19 +3853,18 @@
   %7D%0A
+%0A
   pr
-otected
+ivate
  Str

Fix method visibility and trappy factory method.