Exemple #1
1
  /**
   * When looking at a result set that consists of a Map/HashTable we cannot rely on the output
   * order, as the hashing algorithm or other aspects of the implementation may be different on
   * differnt JDKs or platforms. Hence we take the Map, convert the keys to a List, sort them and
   * Stringify the Map, which is a bit of a hack, but guarantees that we get the same order on all
   * systems. We assume that the keys are strings.
   *
   * @param m The Map that contains keys we wish to return in sorted order
   * @return A string that represents all the keys in sorted order.
   */
  public String sortMapToString(Map m) {

    System.out.println("Map toString looks like: " + m.toString());
    // Pass in crap, and get nothing back
    //
    if (m == null) {
      return null;
    }

    // Sort the keys in the Map
    //
    TreeMap nset = new TreeMap(m);

    System.out.println("Tree map looks like: " + nset.toString());
    return nset.toString();
  }
 public void addNeighbour(String neighbour) {
   if (!addNeighbours) return;
   if (!instanceNeighbourMap.containsKey(neighbour)) return;
   this.instanceNeighbourMap.put(neighbour, "1");
   if (stripEmpty) neighbourMap.put(neighbour, "1");
   logger.debug(name + ": has neighbours: " + instanceNeighbourMap.toString());
 }
  public static void main(String[] args) throws IOException, InterruptedException {
    PerThreadDataSource db = new PerThreadDataSource("jdbc:mysql://localhost/rssminer", "feng", "");
    Searcher.initGlobalSearcher("/var/rssminer/index", db);

    init();

    AtomicInteger id = new AtomicInteger(0);
    BlockingQueue<Result> done = new ArrayBlockingQueue<Result>(100);

    int processors = Runtime.getRuntime().availableProcessors();
    for (int i = 0; i <= processors; i++) {
      Worker w = new Worker(feedhashes, id, done);
      w.setName("worker-" + i);
      w.setDaemon(true);
      w.start();
    }
    int counter[] = new int[65];
    while (id.get() < feedhashes.length) {
      Result r = done.take();
      int innerCounter[] = r.counter;

      for (int i = 0; i < 4; ++i) {
        int d = innerCounter[i];
        if (d > 0) {
          Int c = distCounter.get(d);
          if (c == null) {
            c = new Int();
            distCounter.put(d, c);
          }
          c.i += 1;
        }
      }

      for (int j = 0; j < innerCounter.length; j++) {
        counter[j] += innerCounter[j];
      }
      if (r.id % 5000 == 0) {
        logger.info(r.id + ": " + Arrays.toString(counter));
        // logger.info("{}: {}", r.id, distCounter.toString());
      }
    }
    FileOutputStream fso = new FileOutputStream("/tmp/result");
    fso.write(Arrays.toString(counter).getBytes());
    fso.write("\n\n".getBytes());
    fso.write(distCounter.toString().getBytes());
    // logger.info();
    // logger.info();
  }
Exemple #4
0
  public String getThreadStatusReport() {
    TreeMap<String, Integer> status_map = new TreeMap<String, Integer>();
    status_map.put("STALLED", 0);
    for (StatusContext t : save_thread_list) {
      String status = t.getStatus();
      if (!status_map.containsKey(status)) {
        status_map.put(status, 0);
      }
      status_map.put(status, status_map.get(status) + 1);

      long age = System.currentTimeMillis() - t.getLastStatusChangeTime();
      if (age > 120000L) {
        status_map.put("STALLED", status_map.get("STALLED") + 1);
      }
    }
    return status_map.toString();
  }
  public static void main(String[] args) {
    int x;
    double sq;

    ArrayList<Integer> arr = new ArrayList<Integer>();
    HashMap<Integer, Double> hm = new HashMap<Integer, Double>();
    TreeMap<Integer, Double> tm = new TreeMap<Integer, Double>();
    LinkedHashMap<Integer, Double> lhm = new LinkedHashMap<Integer, Double>();

    for (int i = 0; i < 10; i++) {
      x = (int) (Math.random() * 10);
      sq = x * x;
      arr.add(x);
      hm.put(x, sq);
      tm.put(x, sq);
      lhm.put(x, sq);
    }

    System.out.println("array: " + "\t" + "\t" + arr.toString());
    System.out.println("HashMap: " + "\t" + hm.toString());
    System.out.println("TreeMap: " + "\t" + tm.toString());
    System.out.println("LinkedHashMap:" + "\t" + lhm.toString());
  }
 @Override
 public String toString() {
   return getCommodity() + "=" + prices.toString();
 }
 public NO findLeading(String name) {
   /*
    OBS experimentell metod
    klarar inmatning typ "bevä" som ger Beväringsgatan
    man måste mata in tillräckligt många tecken för att det skall vara
    ett unikt namn (och trycka return)
    detta är dyrbart och inte optimalt så använd den inte i onödan
    tex inte när du bygger upp grafen
    behövs egentligen inte för labben, find ovan räcker
    @TODO felokänslig typ "bevaring" ger Beväringsgatan
   */
   // make sure that name is lower case
   // name = name.toLowerCase();
   // try to lookup the name
   NO tmpNode = fromName.get(name);
   if (tmpNode != null) { // direct hit
     return tmpNode;
   } else {
     // here is the challenge
     // we didn't find the name, try to find a substring
     // this is expensive...
     // we want to create an array of the available stations
     // make a string of the map
     String tmp = fromName.toString();
     // take away {} in the beginning and the end
     tmp = tmp.substring(1, tmp.length() - 1);
     // split the string
     String[] tmpArr = tmp.split(", ");
     // now we have an array with the strings twice
     // like this "station=station"
     // make it an array with only the station i.e. "station"
     for (int i = 0; i < tmpArr.length; i++) {
       tmpArr[i] = tmpArr[i].split("=")[1];
     }
     // make sure data is sorted since a TreeMap can't sort rigth!!!!
     // TODO fix this
     Arrays.sort(tmpArr);
     /*
     Now we do a binary search for the station. We don't expect to find
     it but we get the position where it should be.
     So we can search in the neighbourhood .
     @return index of the search key, if it is contained in the list; otherwise,
     (-(insertion point) - 1). (So this is what we expect to get.)
     The insertion point is defined as the point
     at which the key would be inserted into the list: the index of the
     first element greater than the key, or list.size(), if all elements
     in the list are less than the specified key. Note that this guarantees
     that the return value will be >= 0 if and only if the key is found.
     */
     // name = name.toLowerCase();
     int pos = Arrays.binarySearch(tmpArr, name);
     pos = Math.abs(pos + 1);
     // ===========================================================
     /* Debug start
     	// print tmpArr with its position in the array
     	int i = 0;
     	System.out.println("tmpArr= ");
     	for (String p : tmpArr ) {
     		System.out.println( "" + i++ + " *" + p + "*");
     	}
     	System.out.println("****" + pos);    // debug
     	if ( pos<tmpArr.length ) {
     		System.out.println(tmpArr[pos]);     // debug
     	}
     	System.out.println("name= " + name);  // debug
     */
     // Debug end
     // ===========================================================
     int l = name.length();
     if (pos >= tmpArr.length // outside table
         // long input strings are not ok i.e name="beväqewfqwefqwef"
         || l > tmpArr[pos].length()) {
       return null;
     }
     /*
     Om vi nu antar att name="ull" och att tabellen ser ut så här
     ...
     116 *UlleviNorra*
     117 *UlleviSödra*
     ...
     132 *ÖstraSjukhuset*
     ....
     så kommer pos att vara = 116, första större än "ull"
     om tmp[pos] == "ull" och tmp[pos+1] != "ull" så har vi en träff
     men om tmp[pos] == "ull" och tmp[pos+1] == "ull" så kan vi inte skilja på 116/117.
     om det inte är den sista (dvs pos==tmpArr.length-1) för då har vi ändå en träff.
     Sista alternativet är om vi söker efter "SahlgrenskaHuvuden"
     och har tabellen
     95 *SahlgrenskaHuvudentre*
     96 *Saltholmen*
     då kollar vi så längden på det vi söker inte är större än innehållet i
     nästa position innan vi jämför nästa position (l>tmpArr[pos+1].length)
     */
     if (tmpArr[pos].substring(0, l).equals(name) // found?
         && (pos == tmpArr.length - 1 // sista?
             || l > tmpArr[pos + 1].length() // nästa station för "lång"?
             || !(tmpArr[pos + 1].substring(0, l).equals(name))) // nästa lika?
     ) {
       return find(tmpArr[pos]);
     } else {
       return null;
     }
   }
 }
 public String toString() {
   return fromName.toString();
 }
 public String listarPublicaciones() {
   return publicaciones
       .toString(); // Van a salir todas las listas en una línea, ordenadas por ISBN. Sé que
                    // visualmente será horrible, pero no tengo más tiempo para hacer una interfaz
                    // gráfica más bonita.
 }
 public String toString() {
   return map.toString();
 }