Пример #1
0
  public void serialize(final DataOutput output) {
    try {
      output.writeDouble(maxError);
      output.writeDouble(alpha);
      output.writeLong(landmarkInSeconds);
      output.writeLong(min);
      output.writeLong(max);
      output.writeInt(totalNodeCount);

      postOrderTraversal(
          root,
          new Callback() {
            @Override
            public boolean process(Node node) {
              try {
                serializeNode(output, node);
              } catch (IOException e) {
                Throwables.propagate(e);
              }
              return true;
            }
          });
    } catch (IOException e) {
      Throwables.propagate(e);
    }
  }
 @Test
 public void should_return_stacktrace_as_String() {
   final Throwable throwable = new Throwable("some message");
   assertThat(Throwables.getStackTrace(throwable))
       .isInstanceOf(String.class)
       .contains("java.lang.Throwable: some message")
       .containsPattern("\tat .*\\(Throwables_getStackTrace_Test.java:\\d");
 }
Пример #3
0
 /**
  * Naive implementation of execution with retry logic. a callable will be executed and retry
  * attempted in current thread if the result and exception predicates. before retry, a callable
  * can be executed that can abort the retry and finish the function with the previous result.
  */
 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
 public static <T, EX extends Exception> T executeWithRetry(
     final RetryCallable<T, EX> what,
     final RetryPredicate<? super T> retryOnReturnVal,
     final RetryPredicate<Exception> retryOnException)
     throws InterruptedException, EX {
   T result = null;
   Exception lastEx = null; // last exception
   try {
     result = what.call();
   } catch (InterruptedException ex1) {
     throw what.lastException(ex1);
   } catch (Exception e) { // only EX and RuntimeException
     lastEx = e;
   }
   Exception lastExChain = lastEx; // last exception chained with all previous exceptions
   while ((lastEx != null && retryOnException.apply(lastEx) == Action.RETRY)
       || retryOnReturnVal.apply(result) == Action.RETRY) {
     if (Thread.interrupted()) {
       Thread.currentThread().interrupt();
       throw what.lastException(new InterruptedException());
     }
     result = null;
     lastEx = null;
     try {
       result = what.call();
     } catch (InterruptedException ex1) {
       throw what.lastException(ex1);
     } catch (Exception e) { // only EX and RuntimeException
       lastEx = e;
       if (lastExChain != null) {
         lastExChain = Throwables.suppress(e, lastExChain);
       } else {
         lastExChain = e;
       }
     }
   }
   if (lastEx != null) {
     if (lastExChain instanceof RuntimeException) {
       throw what.lastException((RuntimeException) lastExChain);
     } else {
       throw what.lastException((EX) lastExChain);
     }
   }
   return what.lastReturn(result);
 }
Пример #4
0
  public static QuantileDigest deserialize(DataInput input) {
    try {
      double maxError = input.readDouble();
      double alpha = input.readDouble();

      QuantileDigest result = new QuantileDigest(maxError, alpha);

      result.landmarkInSeconds = input.readLong();
      result.min = input.readLong();
      result.max = input.readLong();
      result.totalNodeCount = input.readInt();

      Deque<Node> stack = new ArrayDeque<>();
      for (int i = 0; i < result.totalNodeCount; i++) {
        int flags = input.readByte();

        Node node = deserializeNode(input);

        if ((flags & Flags.HAS_RIGHT) != 0) {
          node.right = stack.pop();
        }

        if ((flags & Flags.HAS_LEFT) != 0) {
          node.left = stack.pop();
        }

        stack.push(node);
        result.weightedCount += node.weightedCount;
        if (node.weightedCount >= ZERO_WEIGHT_THRESHOLD) {
          result.nonZeroNodeCount++;
        }
      }

      if (!stack.isEmpty()) {
        Preconditions.checkArgument(
            stack.size() == 1, "Tree is corrupted. Expected a single root node");
        result.root = stack.pop();
      }

      return result;
    } catch (IOException e) {
      throw Throwables.propagate(e);
    }
  }
Пример #5
0
  /**
   * Finds directories and files within a given directory and its subdirectories.
   *
   * @param classLoader
   * @param rootPath the root directory, for example org/sonar/sqale, or a file in this root
   *     directory, for example org/sonar/sqale/index.txt
   * @param
   * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo",
   *     "org/sonar/sqale/foo/bar.txt}. Never null.
   */
  public static Collection<String> listResources(
      ClassLoader classLoader, String rootPath, Predicate<String> predicate) {
    String jarPath = null;
    JarFile jar = null;
    try {
      Collection<String> paths = Lists.newArrayList();
      rootPath = StringUtils.removeStart(rootPath, "/");

      URL root = classLoader.getResource(rootPath);
      if (root != null) {
        checkJarFile(root);

        // Path of the root directory
        // Examples :
        // org/sonar/sqale/index.txt  -> rootDirectory is org/sonar/sqale
        // org/sonar/sqale/  -> rootDirectory is org/sonar/sqale
        // org/sonar/sqale  -> rootDirectory is org/sonar/sqale
        String rootDirectory = rootPath;
        if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) {
          rootDirectory = StringUtils.substringBeforeLast(rootPath, "/");
        }
        jarPath =
            root.getPath().substring(5, root.getPath().indexOf("!")); // strip out only the JAR file
        jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8));
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
          String name = entries.nextElement().getName();
          if (name.startsWith(rootDirectory) && predicate.apply(name)) {
            paths.add(name);
          }
        }
      }
      return paths;
    } catch (Exception e) {
      throw Throwables.propagate(e);
    } finally {
      closeJar(jar, jarPath);
    }
  }
 @Test
 public void calls_printStackTrace_with_temp_PrintWriter() {
   final Throwable mock = mock(Throwable.class);
   Throwables.getStackTrace(mock);
   verify(mock, times(1)).printStackTrace(isA(PrintWriter.class));
 }