/**
   * @param fld Folder with files to match.
   * @param ptrn Pattern to match against file name.
   * @return Collection of matched files.
   */
  public static List<VisorLogFile> matchedFiles(File fld, final String ptrn) {
    List<VisorLogFile> files =
        fileTree(
            fld,
            MAX_FOLDER_DEPTH,
            new FileFilter() {
              @Override
              public boolean accept(File f) {
                return !f.isHidden()
                    && (f.isDirectory() || f.isFile() && f.getName().matches(ptrn));
              }
            });

    Collections.sort(files, LAST_MODIFIED);

    return files;
  }
  /**
   * Finds all files in folder and in it's sub-tree of specified depth.
   *
   * @param file Starting folder
   * @param maxDepth Depth of the tree. If 1 - just look in the folder, no sub-folders.
   * @param filter file filter.
   * @return List of found files.
   */
  public static List<VisorLogFile> fileTree(File file, int maxDepth, @Nullable FileFilter filter) {
    if (file.isDirectory()) {
      File[] files = (filter == null) ? file.listFiles() : file.listFiles(filter);

      if (files == null) return Collections.emptyList();

      List<VisorLogFile> res = new ArrayList<>(files.length);

      for (File f : files) {
        if (f.isFile() && f.length() > 0) res.add(new VisorLogFile(f));
        else if (maxDepth > 1) res.addAll(fileTree(f, maxDepth - 1, filter));
      }

      return res;
    }

    return F.asList(new VisorLogFile(file));
  }
Beispiel #3
0
  private void coordinator() throws InterruptedException {
    List<ActiveEvent> activeEvents = new ArrayList<>();
    long curTime = System.currentTimeMillis();
    for (User user : users) {
      if (user.isActive()) {
        for (Event event : user.getEvents()) {
          if (event.getDate().getTime() >= curTime) {
            activeEvents.add(new ActiveEvent(user.getName(), event.getDate(), event.getText()));
          }
        }
      }
    }
    Collections.sort(activeEvents);

    for (int i = 0; i < activeEvents.size(); i++) {
      if (i == 0) {
        Thread.sleep(activeEvents.get(i).getTime() - System.currentTimeMillis());
      } else {
        Thread.sleep(activeEvents.get(i).getTime() - activeEvents.get(i - 1).getTime());
      }
      activeEvents.get(i).print();
    }
  }
Beispiel #4
0
  public static void main(String[] args) throws IOException {
    int i, j, k, a, b, c, d, e;
    String s, s1, s2;
    String[] t;

    boolean done = false;
    TreeMap<String, ArrayList<String>> genre = new TreeMap<>();
    TreeMap<String, ArrayList<String>> author = new TreeMap<>();
    ArrayList<String> list = new ArrayList<>();
    ArrayList<String> temp = new ArrayList<>();
    ArrayList<String> temp2 = new ArrayList<>();
    ArrayList<String> purchased = new ArrayList<>();

    Scanner input = new Scanner(new File("alsopurchased.dat"));
    PrintWriter output = new PrintWriter(new File("alsopurchased.out"));

    for (i = 0; i < 100; i++) {
      s = input.nextLine().trim();
      t = s.split("[*]");
      if (genre.get(t[2]) == null) {
        list = new ArrayList<String>();
        list.add(t[0]);
        genre.put(t[2], list);
      } else {
        list = genre.get(t[2]);
        list.add(t[0]);
        genre.put(t[2], list);
      }

      if (author.get(t[1]) == null) {
        list = new ArrayList<String>();
        list.add(t[0]);
        author.put(t[1], list);

      } else {
        list = author.get(t[1]);
        list.add(t[0]);
        author.put(t[1], list);
      }
    }

    //	out.println(genre+"\n\n");
    //	out.println(author);
    s = input.nextLine().trim();
    a = Integer.parseInt(s);

    for (b = 0; b < a; b++) {
      s = input.nextLine().trim();
      temp = new ArrayList<String>();
      temp2 = new ArrayList<String>();
      purchased = new ArrayList<String>();

      out.println(s);
      output.println(s);
      out.println("Customers also purchased:");
      output.println("Customers also purchased:");

      for (String ss : author.keySet()) {
        list = author.get(ss);
        if (list.contains(s)) {
          temp = author.get(ss);
          break;
        }
      }

      temp.remove(s);
      Collections.sort(temp);
      // out.println("   --"+temp);

      for (String ss : genre.keySet()) {
        list = genre.get(ss);
        if (list.contains(s)) {
          temp2 = genre.get(ss);
          break;
        }
      }

      temp2.remove(s);
      Collections.sort(temp2);
      // out.println("    --"+temp2);
      c = temp.size();
      if (c >= 3) {
        for (d = 0; d < 3; d++) {
          purchased.add(temp.get(d));
          // out.println(temp.get(d));
          // output.println(temp.get(d));

        }
      } else {
        for (d = 0; d < temp.size(); d++) {
          purchased.add(temp.get(d));
          // out.println(temp.get(d));
          // output.println(temp.get(d));
        }
        // out.println(" -- temp2.size()"+temp2.size());
        e = Math.min(3 - temp.size(), temp2.size());
        // e=3-temp.size();
        // out.println("C,E == "+c+","+e);
        for (d = 0; d < e; d++) {
          if (!purchased.contains(temp2.get(d))) purchased.add(temp2.get(d));

          // out.println(temp2.get(d));
          // output.println(temp2.get(d));
        }
      }
      for (String ss : purchased) {
        out.println(ss);
        output.println(ss);
      }

      out.println();
      output.println();
    }

    input.close();
    output.close();
  }
  /**
   * Grabs local events and detects if events was lost since last poll.
   *
   * @param ignite Target grid.
   * @param evtOrderKey Unique key to take last order key from node local map.
   * @param evtThrottleCntrKey Unique key to take throttle count from node local map.
   * @param evtTypes Event types to collect.
   * @param evtMapper Closure to map grid events to Visor data transfer objects.
   * @return Collections of node events
   */
  public static Collection<VisorGridEvent> collectEvents(
      Ignite ignite,
      String evtOrderKey,
      String evtThrottleCntrKey,
      final int[] evtTypes,
      IgniteClosure<Event, VisorGridEvent> evtMapper) {
    assert ignite != null;
    assert evtTypes != null && evtTypes.length > 0;

    ConcurrentMap<String, Long> nl = ignite.cluster().nodeLocalMap();

    final long lastOrder = getOrElse(nl, evtOrderKey, -1L);
    final long throttle = getOrElse(nl, evtThrottleCntrKey, 0L);

    // When we first time arrive onto a node to get its local events,
    // we'll grab only last those events that not older than given period to make sure we are
    // not grabbing GBs of data accidentally.
    final long notOlderThan = System.currentTimeMillis() - EVENTS_COLLECT_TIME_WINDOW;

    // Flag for detecting gaps between events.
    final AtomicBoolean lastFound = new AtomicBoolean(lastOrder < 0);

    IgnitePredicate<Event> p =
        new IgnitePredicate<Event>() {
          /** */
          private static final long serialVersionUID = 0L;

          @Override
          public boolean apply(Event e) {
            // Detects that events were lost.
            if (!lastFound.get() && (lastOrder == e.localOrder())) lastFound.set(true);

            // Retains events by lastOrder, period and type.
            return e.localOrder() > lastOrder
                && e.timestamp() > notOlderThan
                && F.contains(evtTypes, e.type());
          }
        };

    Collection<Event> evts = ignite.events().localQuery(p);

    // Update latest order in node local, if not empty.
    if (!evts.isEmpty()) {
      Event maxEvt = Collections.max(evts, EVTS_ORDER_COMPARATOR);

      nl.put(evtOrderKey, maxEvt.localOrder());
    }

    // Update throttle counter.
    if (!lastFound.get())
      nl.put(evtThrottleCntrKey, throttle == 0 ? EVENTS_LOST_THROTTLE : throttle - 1);

    boolean lost = !lastFound.get() && throttle == 0;

    Collection<VisorGridEvent> res = new ArrayList<>(evts.size() + (lost ? 1 : 0));

    if (lost) res.add(new VisorGridEventsLost(ignite.cluster().localNode().id()));

    for (Event e : evts) {
      VisorGridEvent visorEvt = evtMapper.apply(e);

      if (visorEvt != null) res.add(visorEvt);
    }

    return res;
  }