/** {@inheritDoc} */
  @Nullable
  @Override
  public <T> T value(CacheObjectContext ctx, boolean cpy) {
    if (cpy) return (T) Arrays.copyOf(val, val.length);

    return (T) val;
  }
 /** {@inheritDoc} */
 @Override
 public String toString() {
   return S.toString(
       GridNearAtomicUpdateRequest.class,
       this,
       "filter",
       Arrays.toString(filter),
       "parent",
       super.toString());
 }
Ejemplo n.º 3
0
  /**
   * 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());
  }
Ejemplo n.º 4
0
  /**
   * Concat arrays in one.
   *
   * @param arrays Arrays.
   * @return Summary array.
   */
  public static int[] concat(int[]... arrays) {
    assert arrays != null;
    assert arrays.length > 1;

    int len = 0;

    for (int[] a : arrays) len += a.length;

    int[] r = Arrays.copyOf(arrays[0], len);

    for (int i = 1, shift = 0; i < arrays.length; i++) {
      shift += arrays[i - 1].length;
      System.arraycopy(arrays[i], 0, r, shift, arrays[i].length);
    }

    return r;
  }
Ejemplo n.º 5
0
  /**
   * Run command in separated console.
   *
   * @param workFolder Work folder for command.
   * @param args A string array containing the program and its arguments.
   * @return Started process.
   * @throws IOException If failed to start process.
   */
  public static Process openInConsole(@Nullable File workFolder, String... args)
      throws IOException {
    String[] commands = args;

    String cmd = F.concat(Arrays.asList(args), " ");

    if (U.isWindows()) commands = F.asArray("cmd", "/c", String.format("start %s", cmd));

    if (U.isMacOs())
      commands =
          F.asArray(
              "osascript",
              "-e",
              String.format("tell application \"Terminal\" to do script \"%s\"", cmd));

    if (U.isUnix()) commands = F.asArray("xterm", "-sl", "1024", "-geometry", "200x50", "-e", cmd);

    ProcessBuilder pb = new ProcessBuilder(commands);

    if (workFolder != null) pb.directory(workFolder);

    return pb.start();
  }
    /** @param lsnr Listener. */
    void add(IgniteInClosure<? super IgniteInternalFuture<R>> lsnr) {
      arr = Arrays.copyOf(arr, arr.length + 1);

      arr[arr.length - 1] = lsnr;
    }
  /**
   * JUnit.
   *
   * @throws Exception If failed.
   */
  public void testCollectionMethods() throws Exception {
    // Random queue name.
    String queueName = UUID.randomUUID().toString();

    IgniteQueue<SameHashItem> queue = grid(0).queue(queueName, 0, config(false));

    int retries = 100;

    // Initialize queue.
    for (int i = 0; i < retries; i++)
      queue.addAll(
          Arrays.asList(
              new SameHashItem(Integer.toString(i)), new SameHashItem(Integer.toString(i))));

    // Get arrays from queue.
    assertEquals(retries * 2, queue.toArray().length);

    SameHashItem[] arr2 = new SameHashItem[retries * 3];

    Object[] arr3 = queue.toArray(arr2);

    assertEquals(arr2, arr3);
    assertEquals(arr3[0], new SameHashItem("0"));

    // Check queue items.
    assertEquals(retries * 2, queue.size());

    assertTrue(queue.contains(new SameHashItem(Integer.toString(14))));

    assertFalse(queue.contains(new SameHashItem(Integer.toString(144))));

    Collection<SameHashItem> col1 =
        Arrays.asList(
            new SameHashItem(Integer.toString(14)),
            new SameHashItem(Integer.toString(14)),
            new SameHashItem(Integer.toString(18)));

    assertTrue(queue.containsAll(col1));

    Collection<SameHashItem> col2 =
        Arrays.asList(
            new SameHashItem(Integer.toString(245)),
            new SameHashItem(Integer.toString(14)),
            new SameHashItem(Integer.toString(18)));

    assertFalse(queue.containsAll(col2));

    // Try to remove item.
    assertTrue(queue.remove(new SameHashItem(Integer.toString(14))));

    assertEquals((retries * 2) - 1, queue.size());

    assertTrue(queue.contains(new SameHashItem(Integer.toString(14))));

    assertTrue(queue.remove(new SameHashItem(Integer.toString(14))));

    assertEquals((retries - 1) * 2, queue.size());

    assertFalse(queue.remove(new SameHashItem(Integer.toString(14))));

    // Try to remove some items.
    assertTrue(queue.contains(new SameHashItem(Integer.toString(33))));

    assertTrue(
        queue.removeAll(
            Arrays.asList(
                new SameHashItem(Integer.toString(15)),
                new SameHashItem(Integer.toString(14)),
                new SameHashItem(Integer.toString(33)),
                new SameHashItem(Integer.toString(1)))));

    assertFalse(queue.contains(new SameHashItem(Integer.toString(33))));

    // Try to retain all items.
    assertTrue(
        queue.retainAll(
            Arrays.asList(
                new SameHashItem(Integer.toString(15)),
                new SameHashItem(Integer.toString(14)),
                new SameHashItem(Integer.toString(33)),
                new SameHashItem(Integer.toString(1)))));

    assertFalse(queue.contains(new SameHashItem(Integer.toString(2))));

    assert queue.isEmpty();
  }
Ejemplo n.º 8
0
 /** {@inheritDoc} */
 @Override
 public Collection<UUID> masterNodeIds() {
   return Arrays.asList(nodeId, nearNodeId);
 }