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); } }
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); } }
/** * 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); } }