示例#1
0
文件: Macro.java 项目: bramk/bnd
 private String doCommand(Object target, String method, String[] args) {
   if (target == null) ; // System.err.println("Huh? Target should never be null " +
   // domain);
   else {
     String cname = "_" + method.replaceAll("-", "_");
     try {
       Method m = target.getClass().getMethod(cname, new Class[] {String[].class});
       return (String) m.invoke(target, new Object[] {args});
     } catch (NoSuchMethodException e) {
       // Ignore
     } catch (InvocationTargetException e) {
       if (e.getCause() instanceof IllegalArgumentException) {
         domain.error(
             "%s, for cmd: %s, arguments; %s",
             e.getCause().getMessage(), method, Arrays.toString(args));
       } else {
         domain.warning("Exception in replace: %s", e.getCause());
         e.getCause().printStackTrace();
       }
     } catch (Exception e) {
       domain.warning("Exception in replace: " + e + " method=" + method);
       e.printStackTrace();
     }
   }
   return null;
 }
示例#2
0
文件: Macro.java 项目: bramk/bnd
 public static Properties getParent(Properties p) {
   try {
     Field f = Properties.class.getDeclaredField("defaults");
     f.setAccessible(true);
     return (Properties) f.get(p);
   } catch (Exception e) {
     Field[] fields = Properties.class.getFields();
     System.err.println(Arrays.toString(fields));
     return null;
   }
 }
  /**
   * Returns compact class host.
   *
   * @param obj Object to compact.
   * @return String.
   */
  @Nullable
  public static Object compactObject(Object obj) {
    if (obj == null) return null;

    if (obj instanceof Enum) return obj.toString();

    if (obj instanceof String || obj instanceof Boolean || obj instanceof Number) return obj;

    if (obj instanceof Collection) {
      Collection col = (Collection) obj;

      Object[] res = new Object[col.size()];

      int i = 0;

      for (Object elm : col) res[i++] = compactObject(elm);

      return res;
    }

    if (obj.getClass().isArray()) {
      Class<?> arrType = obj.getClass().getComponentType();

      if (arrType.isPrimitive()) {
        if (obj instanceof boolean[]) return Arrays.toString((boolean[]) obj);
        if (obj instanceof byte[]) return Arrays.toString((byte[]) obj);
        if (obj instanceof short[]) return Arrays.toString((short[]) obj);
        if (obj instanceof int[]) return Arrays.toString((int[]) obj);
        if (obj instanceof long[]) return Arrays.toString((long[]) obj);
        if (obj instanceof float[]) return Arrays.toString((float[]) obj);
        if (obj instanceof double[]) return Arrays.toString((double[]) obj);
      }

      Object[] arr = (Object[]) obj;

      int iMax = arr.length - 1;

      StringBuilder sb = new StringBuilder("[");

      for (int i = 0; i <= iMax; i++) {
        sb.append(compactObject(arr[i]));

        if (i != iMax) sb.append(", ");
      }

      sb.append("]");

      return sb.toString();
    }

    return U.compact(obj.getClass().getName());
  }
示例#4
0
文件: Macro.java 项目: bramk/bnd
 /**
  * Get the contents of a file.
  *
  * @param in
  * @return
  * @throws IOException
  */
 public String _cat(String args[]) throws IOException {
   verifyCommand(args, "${cat;<in>}, get the content of a file", null, 2, 2);
   File f = domain.getFile(args[1]);
   if (f.isFile()) {
     return IO.collect(f);
   } else if (f.isDirectory()) {
     return Arrays.toString(f.list());
   } else {
     try {
       URL url = new URL(args[1]);
       return IO.collect(url, "UTF-8");
     } catch (MalformedURLException mfue) {
       // Ignore here
     }
     return null;
   }
 }
 private InputStream getReport(
     HttpServletRequest request,
     HttpServletResponse response,
     Tab tab,
     TableModel tableModel,
     Integer columnCountLimit)
     throws ServletException, IOException {
   StringBuffer suri = new StringBuffer();
   suri.append("/xava/jasperReport");
   suri.append("?language=");
   suri.append(Locales.getCurrent().getLanguage());
   suri.append("&widths=");
   suri.append(Arrays.toString(getWidths(tableModel)));
   if (columnCountLimit != null) {
     suri.append("&columnCountLimit=");
     suri.append(columnCountLimit);
   }
   response.setCharacterEncoding(XSystem.getEncoding());
   return Servlets.getURIAsStream(request, response, suri.toString());
 }
示例#6
0
文件: Macro.java 项目: bramk/bnd
  public String _tstamp(String args[]) {
    String format = "yyyyMMddHHmm";
    long now = System.currentTimeMillis();
    TimeZone tz = TimeZone.getTimeZone("UTC");

    if (args.length > 1) {
      format = args[1];
    }
    if (args.length > 2) {
      tz = TimeZone.getTimeZone(args[2]);
    }
    if (args.length > 3) {
      now = Long.parseLong(args[3]);
    }
    if (args.length > 4) {
      domain.warning("Too many arguments for tstamp: " + Arrays.toString(args));
    }

    SimpleDateFormat sdf = new SimpleDateFormat(format);
    sdf.setTimeZone(tz);

    return sdf.format(new Date(now));
  }
  public double calc(int[] hand, int[] sothe) {
    double ret = 0;
    int len = hand.length;
    boolean[] used = new boolean[len];

    // fill the solid
    for (int i = 0; i < len; i++) {
      if (sothe[i] == -1) continue;

      // pick lowest winner
      int min = INF;
      int pos = -1;
      for (int j = 0; j < len; j++) {
        if (hand[j] > sothe[i] && !used[j] && hand[j] < min) {
          min = hand[j];
          pos = j;
        }
      }
      if (min != INF) {
        used[pos] = true;
        ret += 1;
        continue;
      }

      // pick lowest loser
      min = INF;
      pos = -1;
      for (int j = 0; j < len; j++) {
        if (!used[j] && hand[j] < min) {
          min = hand[j];
          pos = j;
        }
      }
      used[pos] = true;
    }
    System.out.println(Arrays.toString(used));

    // get the -1 hands
    List<Integer> sotheHands = new ArrayList<>();
    for (int i = 1; i <= 2 * len; i++) {
      boolean appears = false;
      for (int j = 0; j < len; j++) {
        if (hand[j] == i || sothe[j] == i) {
          appears = true;
          break;
        }
      }
      if (!appears) sotheHands.add(i);
    }

    // calculate the unknown
    for (int sotheHand : sotheHands) {
      int wincount = 0;
      int losecount = 0;
      for (int j = 0; j < len; j++) {
        if (used[j]) continue;
        if (sotheHand > hand[j]) {
          losecount++;
        } else {
          wincount++;
        }
      }
      if (wincount + losecount != 0) {
        ret += (double) wincount / (double) (wincount + losecount);
      }
    }

    return ret;
  }