1/*
2 * Copyright (C) 2012 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.caliper.bridge;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.caliper.util.ShortDuration;
22import com.google.common.base.MoreObjects;
23import com.google.common.base.Objects;
24
25/**
26 * A message representing output produced by the JVM when {@code -XX:+PrintGC} is enabled.
27 */
28public final class GcLogMessage extends LogMessage {
29  /**
30   * The type of the garbage collection performed.
31   */
32  public static enum Type {
33    FULL,
34    INCREMENTAL,
35  }
36
37  private final Type type;
38  private final ShortDuration duration;
39
40  GcLogMessage(Type type, ShortDuration duration) {
41    this.type = checkNotNull(type);
42    this.duration = checkNotNull(duration);
43  }
44
45  public Type type() {
46    return type;
47  }
48
49  public ShortDuration duration() {
50    return duration;
51  }
52
53  @Override
54  public void accept(LogMessageVisitor visitor) {
55    visitor.visit(this);
56  }
57
58  @Override
59  public int hashCode() {
60    return Objects.hashCode(type, duration);
61  }
62
63  @Override
64  public boolean equals(Object obj) {
65    if (obj == this) {
66      return true;
67    } else if (obj instanceof GcLogMessage) {
68      GcLogMessage that = (GcLogMessage) obj;
69      return this.type == that.type
70          && this.duration.equals(that.duration);
71    } else {
72      return false;
73    }
74  }
75
76  @Override
77  public String toString() {
78    return MoreObjects.toStringHelper(this)
79        .addValue(type)
80        .add("duration", duration)
81        .toString();
82  }
83}
84