@Test public void testDiffWithAttribution() { WakeLockMetrics metricsA = new WakeLockMetrics(true); metricsA.heldTimeMs = 25l; metricsA.acquiredCount = 100; metricsA.tagTimeMs.put("TestWakeLock1", 15l); metricsA.tagTimeMs.put("TestWakeLock2", 10l); WakeLockMetrics metricsB = new WakeLockMetrics(true); metricsB.heldTimeMs = 20l; metricsB.acquiredCount = 90; metricsB.tagTimeMs.put("TestWakeLock1", 10l); metricsB.tagTimeMs.put("TestWakeLock2", 10l); WakeLockMetrics output = new WakeLockMetrics(true); output.tagTimeMs.put("Test", 1L); output.acquiredCount = 341; output.heldTimeMs = 100L; metricsA.diff(metricsB, output); WakeLockMetrics expectedOutput = new WakeLockMetrics(true); expectedOutput.acquiredCount = 10; expectedOutput.heldTimeMs = 5l; expectedOutput.tagTimeMs.put("TestWakeLock1", 5l); assertThat(output).isEqualTo(expectedOutput); }
@Override public WakeLockMetrics diff(@Nullable WakeLockMetrics b, @Nullable WakeLockMetrics output) { if (output == null) { output = new WakeLockMetrics(isAttributionEnabled); } if (b == null) { output.set(this); } else { output.heldTimeMs = heldTimeMs - b.heldTimeMs; output.acquiredCount = acquiredCount - b.acquiredCount; if (output.isAttributionEnabled) { output.tagTimeMs.clear(); for (int i = 0, size = tagTimeMs.size(); i < size; i++) { String tag = tagTimeMs.keyAt(i); Long currentTimeMs = b.tagTimeMs.get(tag); long difference = tagTimeMs.valueAt(i) - (currentTimeMs == null ? 0 : currentTimeMs); if (difference != 0) { output.tagTimeMs.put(tag, difference); } } } } return output; }
WakeLockMetrics extends SystemMetrics<WakeLockMetrics> { @Override public WakeLockMetrics diff(@Nullable WakeLockMetrics b, @Nullable WakeLockMetrics output) { if (output == null) { output = new WakeLockMetrics(isAttributionEnabled); } if (b == null) { output.set(this); } else { output.heldTimeMs = heldTimeMs - b.heldTimeMs; output.acquiredCount = acquiredCount - b.acquiredCount; if (output.isAttributionEnabled) { output.tagTimeMs.clear(); for (int i = 0, size = tagTimeMs.size(); i < size; i++) { String tag = tagTimeMs.keyAt(i); Long currentTimeMs = b.tagTimeMs.get(tag); long difference = tagTimeMs.valueAt(i) - (currentTimeMs == null ? 0 : currentTimeMs); if (difference != 0) { output.tagTimeMs.put(tag, difference); } } } } return output; } }
WakeLockMetrics extends SystemMetrics<WakeLockMetrics> { @Override public WakeLockMetrics diff(@Nullable WakeLockMetrics b, @Nullable WakeLockMetrics output) { if (output == null) { output = new WakeLockMetrics(isAttributionEnabled); } if (b == null) { output.set(this); } else { output.heldTimeMs = heldTimeMs - b.heldTimeMs; output.acquiredCount = acquiredCount - b.acquiredCount; if (output.isAttributionEnabled) { output.tagTimeMs.clear(); for (int i = 0, size = tagTimeMs.size(); i < size; i++) { String tag = tagTimeMs.keyAt(i); Long currentTimeMs = b.tagTimeMs.get(tag); long difference = tagTimeMs.valueAt(i) - (currentTimeMs == null ? 0 : currentTimeMs); if (difference != 0) { output.tagTimeMs.put(tag, difference); } } } } return output; } WakeLockMetrics();  WakeLockMetrics(boolean isAttributionEnabled); }
WakeLockMetrics extends SystemMetrics<WakeLockMetrics> { @Override public WakeLockMetrics diff(@Nullable WakeLockMetrics b, @Nullable WakeLockMetrics output) { if (output == null) { output = new WakeLockMetrics(isAttributionEnabled); } if (b == null) { output.set(this); } else { output.heldTimeMs = heldTimeMs - b.heldTimeMs; output.acquiredCount = acquiredCount - b.acquiredCount; if (output.isAttributionEnabled) { output.tagTimeMs.clear(); for (int i = 0, size = tagTimeMs.size(); i < size; i++) { String tag = tagTimeMs.keyAt(i); Long currentTimeMs = b.tagTimeMs.get(tag); long difference = tagTimeMs.valueAt(i) - (currentTimeMs == null ? 0 : currentTimeMs); if (difference != 0) { output.tagTimeMs.put(tag, difference); } } } } return output; } WakeLockMetrics();  WakeLockMetrics(boolean isAttributionEnabled); @Override WakeLockMetrics sum(@Nullable WakeLockMetrics b, @Nullable WakeLockMetrics output); @Override WakeLockMetrics diff(@Nullable WakeLockMetrics b, @Nullable WakeLockMetrics output); @Override WakeLockMetrics set(WakeLockMetrics b); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); @Nullable JSONObject attributionToJSONObject(); }
WakeLockMetrics extends SystemMetrics<WakeLockMetrics> { @Override public WakeLockMetrics diff(@Nullable WakeLockMetrics b, @Nullable WakeLockMetrics output) { if (output == null) { output = new WakeLockMetrics(isAttributionEnabled); } if (b == null) { output.set(this); } else { output.heldTimeMs = heldTimeMs - b.heldTimeMs; output.acquiredCount = acquiredCount - b.acquiredCount; if (output.isAttributionEnabled) { output.tagTimeMs.clear(); for (int i = 0, size = tagTimeMs.size(); i < size; i++) { String tag = tagTimeMs.keyAt(i); Long currentTimeMs = b.tagTimeMs.get(tag); long difference = tagTimeMs.valueAt(i) - (currentTimeMs == null ? 0 : currentTimeMs); if (difference != 0) { output.tagTimeMs.put(tag, difference); } } } } return output; } WakeLockMetrics();  WakeLockMetrics(boolean isAttributionEnabled); @Override WakeLockMetrics sum(@Nullable WakeLockMetrics b, @Nullable WakeLockMetrics output); @Override WakeLockMetrics diff(@Nullable WakeLockMetrics b, @Nullable WakeLockMetrics output); @Override WakeLockMetrics set(WakeLockMetrics b); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); @Nullable JSONObject attributionToJSONObject(); public boolean isAttributionEnabled; final SimpleArrayMap<String, Long> tagTimeMs; public long heldTimeMs; public long acquiredCount; }