killbill-aplcache

usage: add POJOs around sources Signed-off-by: Pierre-Alexandre

7/28/2012 6:11:19 PM

Details

diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceAndId.java b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceAndId.java
new file mode 100644
index 0000000..424f8be
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceAndId.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.usage.timeline.sources;
+
+/**
+ * This class represents one row in the sources table
+ */
+public class SourceAndId {
+
+    private final String source;
+    private final int sourceId;
+
+    public SourceAndId(final String source, final int sourceId) {
+        this.source = source;
+        this.sourceId = sourceId;
+    }
+
+    public String getSource() {
+        return source;
+    }
+
+    public int getSourceId() {
+        return sourceId;
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append("SourceAndId");
+        sb.append("{source='").append(source).append('\'');
+        sb.append(", sourceId=").append(sourceId);
+        sb.append('}');
+        return sb.toString();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final SourceAndId that = (SourceAndId) o;
+
+        if (sourceId != that.sourceId) {
+            return false;
+        }
+        if (source != null ? !source.equals(that.source) : that.source != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = source != null ? source.hashCode() : 0;
+        result = 31 * result + sourceId;
+        return result;
+    }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceIdAndMetricId.java b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceIdAndMetricId.java
new file mode 100644
index 0000000..087eb45
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceIdAndMetricId.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.usage.timeline.sources;
+
+public class SourceIdAndMetricId {
+
+    private final int sourceId;
+    private final int metricId;
+
+    public SourceIdAndMetricId(final int sourceId, final int metricId) {
+        this.sourceId = sourceId;
+        this.metricId = metricId;
+    }
+
+    public int getSourceId() {
+        return sourceId;
+    }
+
+    public int getMetricId() {
+        return metricId;
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append("SourceIdAndMetricId");
+        sb.append("{sourceId=").append(sourceId);
+        sb.append(", metricId=").append(metricId);
+        sb.append('}');
+        return sb.toString();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final SourceIdAndMetricId that = (SourceIdAndMetricId) o;
+
+        if (metricId != that.metricId) {
+            return false;
+        }
+        if (sourceId != that.sourceId) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = sourceId;
+        result = 31 * result + metricId;
+        return result;
+    }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceIdAndMetricIdMapper.java b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceIdAndMetricIdMapper.java
new file mode 100644
index 0000000..ba60a5a
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceIdAndMetricIdMapper.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.usage.timeline.sources;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.skife.jdbi.v2.StatementContext;
+import org.skife.jdbi.v2.tweak.ResultSetMapper;
+
+import com.ning.billing.usage.timeline.sources.SourceIdAndMetricId;
+
+public class SourceIdAndMetricIdMapper implements ResultSetMapper<SourceIdAndMetricId> {
+
+    @Override
+    public SourceIdAndMetricId map(final int index, final ResultSet rs, final StatementContext ctx) throws SQLException {
+        return new SourceIdAndMetricId(rs.getInt("source_id"), rs.getInt("sample_kind_id"));
+    }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceMapper.java b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceMapper.java
new file mode 100644
index 0000000..d8c8c26
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceMapper.java
@@ -0,0 +1,15 @@
+package com.ning.billing.usage.timeline.sources;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.skife.jdbi.v2.StatementContext;
+import org.skife.jdbi.v2.tweak.ResultSetMapper;
+
+public class SourceMapper implements ResultSetMapper<SourceAndId> {
+
+    @Override
+    public SourceAndId map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException {
+        return new SourceAndId(r.getString("source"), r.getInt("id"));
+    }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceSamplesForTimestamp.java b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceSamplesForTimestamp.java
new file mode 100644
index 0000000..62dee6b
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/sources/SourceSamplesForTimestamp.java
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.usage.timeline.sources;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.joda.time.DateTime;
+
+import com.ning.billing.usage.timeline.samples.ScalarSample;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonValue;
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Instances of this class represent samples sent from one source and one
+ * category, e.g., JVM, representing one point in time.
+ */
+@SuppressWarnings("unchecked")
+public class SourceSamplesForTimestamp {
+
+    private static final String KEY_SOURCE = "H";
+    private static final String KEY_CATEGORY = "V";
+    private static final String KEY_TIMESTAMP = "T";
+    private static final String KEY_SAMPLES = "S";
+
+    private final Integer sourceId;
+    private final String category;
+    private final DateTime timestamp;
+    // A map from sample id to sample value for that timestamp
+    private final Map<Integer, ScalarSample> samples;
+
+    public SourceSamplesForTimestamp(final int sourceId, final String category, final DateTime timestamp) {
+        this(sourceId, category, timestamp, new HashMap<Integer, ScalarSample>());
+    }
+
+    @JsonCreator
+    public SourceSamplesForTimestamp(@JsonProperty(KEY_SOURCE) final Integer sourceId, @JsonProperty(KEY_CATEGORY) final String category,
+                                     @JsonProperty(KEY_TIMESTAMP) final DateTime timestamp, @JsonProperty(KEY_SAMPLES) final Map<Integer, ScalarSample> samples) {
+        this.sourceId = sourceId;
+        this.category = category;
+        this.timestamp = timestamp;
+        this.samples = samples;
+    }
+
+    public int getSourceId() {
+        return sourceId;
+    }
+
+    public String getCategory() {
+        return category;
+    }
+
+    public DateTime getTimestamp() {
+        return timestamp;
+    }
+
+    public Map<Integer, ScalarSample> getSamples() {
+        return samples;
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append("SourceSamplesForTimestamp");
+        sb.append("{category='").append(category).append('\'');
+        sb.append(", sourceId=").append(sourceId);
+        sb.append(", timestamp=").append(timestamp);
+        sb.append(", samples=").append(samples);
+        sb.append('}');
+
+        return sb.toString();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final SourceSamplesForTimestamp that = (SourceSamplesForTimestamp) o;
+
+        if (category != null ? !category.equals(that.category) : that.category != null) {
+            return false;
+        }
+        if (samples != null ? !samples.equals(that.samples) : that.samples != null) {
+            return false;
+        }
+        if (sourceId != null ? !sourceId.equals(that.sourceId) : that.sourceId != null) {
+            return false;
+        }
+        if (timestamp != null ? !timestamp.equals(that.timestamp) : that.timestamp != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = sourceId != null ? sourceId.hashCode() : 0;
+        result = 31 * result + (category != null ? category.hashCode() : 0);
+        result = 31 * result + (timestamp != null ? timestamp.hashCode() : 0);
+        result = 31 * result + (samples != null ? samples.hashCode() : 0);
+        return result;
+    }
+
+    @JsonValue
+    public Map<String, Object> toMap() {
+        return ImmutableMap.of(KEY_SOURCE, sourceId, KEY_CATEGORY, category, KEY_TIMESTAMP, timestamp, KEY_SAMPLES, samples);
+    }
+}