<commit_before>/*
 * $Id: StatusTable.java,v 1.11 2003-03-17 21:48:36 troberts Exp $
 */

/*

Copyright (c) 2000-2002 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.daemon.status;

import java.util.*;
import java.net.*;
import org.lockss.util.*;

/**
 * Returned by {@link StatusService#getTable(String, String)} 
 */
public class StatusTable {
  private String name;
  private String key;
  private String title;
  private List columnDescriptors;
  private List rows;
  private List defaultSortRules;
  /**
   * @param name String representing table name
   * @param key String representing the key for this table, may be null
   * @param columnDescriptors List of {@link ColumnDescriptor} objects
   * @param defaultSortRules List of {@link SortRule} objects specifying the 
   * default sort rules for this table.  If null, we'll sort ascending by 
   * first column
   * @param rows List of {@link java.util.Map} objects, representing rows in 
   * this table.  Not assumed to be in any specific order
   */
  protected StatusTable(String name, String key, String title,
			List columnDescriptors, 
			List defaultSortRules, List rows) {
    this.name = name;
    this.columnDescriptors = columnDescriptors;
    this.rows = rows;
    this.defaultSortRules = 
      defaultSortRules == null ? makeDefaultSortRules() : defaultSortRules;
    this.key = key;
    this.title = title;
  }

  private List makeDefaultSortRules() {
    ColumnDescriptor firstCol = (ColumnDescriptor)columnDescriptors.get(0);
    SortRule sortRule = new SortRule(firstCol.getColumnName(), true);
    return ListUtil.list(sortRule);
  }

  /**
   * Get the name of this table
   * @returns name of this table
   */
  public String getName() {
    return name;
  }

  /**
   * Get the key for this table
   * @returns key for this table
   */
  public String getKey() {
    return key;
  }

  /**
   * Get the title for this table
   * @returns title for this table
   */
  public String getTitle() {
    return title;
  }

  /**
   * Gets a list of {@link ColumnDescriptor}s representing the 
   * columns in this table in their preferred display order.
   * @returns list of {@link ColumnDescriptor}s the columns in 
   * the table in the preferred display order
   */
  public List getColumnDescriptors() {
    return columnDescriptors;
  }

  /**
   * Gets a list of {@link java.util.Map} objects for all the rows in the 
   * table in their default sort order.
   * @returns list of {@link java.util.Map}s representing rows in the table 
   * in their default sort order 
   */
  public List getSortedRows() {
    return getSortedRows(defaultSortRules);
  }

  /**
   * Same as getSortedRows(), but will sort according to the rules 
   * specified in sortRules
   * @param sortRules list of {@link StatusTable.SortRule} objects describing
   *  how to sort  the rows
   * @returns list of {@link java.util.Map}s representing rows in the table 
   * in the sort order specified by sortRules 
   */
  public List getSortedRows(List sortRules) {
    Collections.sort(rows, 
		     new SortRuleComparator(sortRules, columnDescriptors));
    return rows;
  }

  public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("[StatusTable:");
    sb.append(name);
    sb.append(", ");
    sb.append(key);
    sb.append(", ");
    sb.append(columnDescriptors);
    sb.append(", ");
    sb.append(rows);
    sb.append("]");
    return sb.toString();
  }

  private static class SortRuleComparator implements Comparator {
    List sortRules;
    Map colTypeHash;

    public SortRuleComparator(List sortRules, List columnDescriptors) {
      this.sortRules = sortRules;
      this.colTypeHash = getTypeHash(columnDescriptors);
    }

    private HashMap getTypeHash(List columnDescriptors) {
      HashMap typeHash = new HashMap(columnDescriptors.size());
      Iterator it = columnDescriptors.iterator();
      while (it.hasNext()) {
	ColumnDescriptor col = (ColumnDescriptor) it.next();
	typeHash.put(col.getColumnName(), new Integer(col.getType()));
      }
      return typeHash;
    }

    public int compare(Object a, Object b) {
      Map rowA = (Map)a;
      Map rowB = (Map)b;
      int returnVal = 0;
      Iterator it = sortRules.iterator();

      while (returnVal == 0 && it.hasNext()){
	SortRule sortRule = (SortRule)it.next();
	String colName = sortRule.getColumnName();
	Object valA = rowA.get(colName);
	Object valB = rowB.get(colName);

	int curType = ((Integer)colTypeHash.get(colName)).intValue();

	switch (curType) {
	case ColumnDescriptor.TYPE_IP_ADDRESS:
	  returnVal = compareInetAddresses((InetAddress)valA, 
					   (InetAddress)valB);
	  break;<commit_msg>added debug info when we get a bad column name while sorting<commit_after>/*
 * $Id: StatusTable.java,v 1.12 2003-03-18 02:27:53 troberts Exp $
 */

/*

Copyright (c) 2000-2002 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.daemon.status;

import java.util.*;
import java.net.*;
import org.lockss.util.*;

/**
 * Returned by {@link StatusService#getTable(String, String)} 
 */
public class StatusTable {
  private String name;
  private String key;
  private String title;
  private List columnDescriptors;
  private List rows;
  private List defaultSortRules;
  private static Logger logger = Logger.getLogger("StatusTable");


  /**
   * @param name String representing table name
   * @param key String representing the key for this table, may be null
   * @param columnDescriptors List of {@link ColumnDescriptor} objects
   * @param defaultSortRules List of {@link SortRule} objects specifying the 
   * default sort rules for this table.  If null, we'll sort ascending by 
   * first column
   * @param rows List of {@link java.util.Map} objects, representing rows in 
   * this table.  Not assumed to be in any specific order
   */
  protected StatusTable(String name, String key, String title,
			List columnDescriptors, 
			List defaultSortRules, List rows) {
    this.name = name;
    this.columnDescriptors = columnDescriptors;
    this.rows = rows;
    this.defaultSortRules = 
      defaultSortRules == null ? makeDefaultSortRules() : defaultSortRules;
    this.key = key;
    this.title = title;
  }

  private List makeDefaultSortRules() {
    ColumnDescriptor firstCol = (ColumnDescriptor)columnDescriptors.get(0);
    SortRule sortRule = new SortRule(firstCol.getColumnName(), true);
    return ListUtil.list(sortRule);
  }

  /**
   * Get the name of this table
   * @returns name of this table
   */
  public String getName() {
    return name;
  }

  /**
   * Get the key for this table
   * @returns key for this table
   */
  public String getKey() {
    return key;
  }

  /**
   * Get the title for this table
   * @returns title for this table
   */
  public String getTitle() {
    return title;
  }

  /**
   * Gets a list of {@link ColumnDescriptor}s representing the 
   * columns in this table in their preferred display order.
   * @returns list of {@link ColumnDescriptor}s the columns in 
   * the table in the preferred display order
   */
  public List getColumnDescriptors() {
    return columnDescriptors;
  }

  /**
   * Gets a list of {@link java.util.Map} objects for all the rows in the 
   * table in their default sort order.
   * @returns list of {@link java.util.Map}s representing rows in the table 
   * in their default sort order 
   */
  public List getSortedRows() {
    return getSortedRows(defaultSortRules);
  }

  /**
   * Same as getSortedRows(), but will sort according to the rules 
   * specified in sortRules
   * @param sortRules list of {@link StatusTable.SortRule} objects describing
   *  how to sort  the rows
   * @returns list of {@link java.util.Map}s representing rows in the table 
   * in the sort order specified by sortRules 
   */
  public List getSortedRows(List sortRules) {
    Collections.sort(rows, 
		     new SortRuleComparator(sortRules, columnDescriptors));
    return rows;
  }

  public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("[StatusTable:");
    sb.append(name);
    sb.append(", ");
    sb.append(key);
    sb.append(", ");
    sb.append(columnDescriptors);
    sb.append(", ");
    sb.append(rows);
    sb.append("]");
    return sb.toString();
  }

  private static class SortRuleComparator implements Comparator {
    List sortRules;
    Map colTypeHash;

    public SortRuleComparator(List sortRules, List columnDescriptors) {
      this.sortRules = sortRules;
      this.colTypeHash = getTypeHash(columnDescriptors);
    }

    private HashMap getTypeHash(List columnDescriptors) {
      HashMap typeHash = new HashMap(columnDescriptors.size());
      Iterator it = columnDescriptors.iterator();
      while (it.hasNext()) {
	ColumnDescriptor col = (ColumnDescriptor) it.next();
	typeHash.put(col.getColumnName(), new Integer(col.getType()));
      }
      return typeHash;
    }

    public int compare(Object a, Object b) {
      Map rowA = (Map)a;
      Map rowB = (Map)b;
      int returnVal = 0;
      Iterator it = sortRules.iterator();

      while (returnVal == 0 && it.hasNext()){
	SortRule sortRule = (SortRule)it.next();
	String colName = sortRule.getColumnName();
	Object valA = rowA.get(colName);
	Object valB = rowB.get(colName);

	
	Integer type = (Integer)colTypeHash.get(colName);
	if (type == null) {
	  //XXX currently this will throw a NullPointerException
	  //XXX leave for debugging, remove before shipping
	  logger.error("Got bad column name: "+colName);
	}

	switch (type.intValue()) {
	case ColumnDescriptor.TYPE_IP_ADDRESS:
	  returnVal = compareInetAddresses((InetAddress)valA, 
					   (InetAddress)valB);
	  break;
{'fraction_non_alphanumeric': 0.0870062030688867, 'fraction_numerical': 0.004570682337577538, 'mean_word_length': 3.551634472511144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6531823', 'n_tokens_mistral': 3525, 'n_tokens_neox': 3398, 'n_words': 1321}