killbill-aplcache

usage: add POJOs around categories Several sources can be

7/28/2012 6:05:20 PM

Details

diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryAndMetrics.java b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryAndMetrics.java
new file mode 100644
index 0000000..c1ba8b5
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryAndMetrics.java
@@ -0,0 +1,108 @@
+/*
+ * 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.categories;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class CategoryAndMetrics implements Comparable<CategoryAndMetrics> {
+
+    @JsonProperty
+    private final String eventCategory;
+    @JsonProperty
+    private final Set<String> metrics = new HashSet<String>();
+
+    public CategoryAndMetrics(final String eventCategory) {
+        this.eventCategory = eventCategory;
+    }
+
+    @JsonCreator
+    public CategoryAndMetrics(@JsonProperty("eventCategory") final String eventCategory, @JsonProperty("metrics") final List<String> metrics) {
+        this.eventCategory = eventCategory;
+        this.metrics.addAll(metrics);
+    }
+
+    public void addMetric(final String metric) {
+        metrics.add(metric);
+    }
+
+    public String getEventCategory() {
+        return eventCategory;
+    }
+
+    public Set<String> getMetrics() {
+        return metrics;
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append("CategoryAndMetrics");
+        sb.append("{eventCategory='").append(eventCategory).append('\'');
+        sb.append(", metrics=").append(metrics);
+        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 CategoryAndMetrics that = (CategoryAndMetrics) o;
+
+        if (!eventCategory.equals(that.eventCategory)) {
+            return false;
+        }
+        if (!metrics.equals(that.metrics)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = eventCategory.hashCode();
+        result = 31 * result + metrics.hashCode();
+        return result;
+    }
+
+    @Override
+    public int compareTo(final CategoryAndMetrics o) {
+        final int categoryComparison = eventCategory.compareTo(o.getEventCategory());
+        if (categoryComparison != 0) {
+            return categoryComparison;
+        } else {
+            if (metrics.size() > o.getMetrics().size()) {
+                return 1;
+            } else if (metrics.size() < o.getMetrics().size()) {
+                return -1;
+            } else {
+                return 0;
+            }
+        }
+    }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryAndMetricsForSources.java b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryAndMetricsForSources.java
new file mode 100644
index 0000000..3126db6
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryAndMetricsForSources.java
@@ -0,0 +1,97 @@
+/*
+ * 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.categories;
+
+import java.util.Set;
+import java.util.TreeSet;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class CategoryAndMetricsForSources implements Comparable<CategoryAndMetricsForSources> {
+
+    @JsonProperty
+    private final CategoryAndMetrics categoryAndMetrics;
+    @JsonProperty
+    private final Set<String> sources;
+
+    public CategoryAndMetricsForSources(final String eventCategory) {
+        this(new CategoryAndMetrics(eventCategory), new TreeSet<String>());
+    }
+
+    @JsonCreator
+    public CategoryAndMetricsForSources(@JsonProperty("categoryAndMetrics") final CategoryAndMetrics categoryAndMetrics, @JsonProperty("sources") final Set<String> sources) {
+        this.categoryAndMetrics = categoryAndMetrics;
+        this.sources = sources;
+    }
+
+    public void add(final String metric, final String source) {
+        categoryAndMetrics.addMetric(metric);
+        sources.add(source);
+    }
+
+    public CategoryAndMetrics getCategoryAndMetrics() {
+        return categoryAndMetrics;
+    }
+
+    public Set<String> getSources() {
+        return sources;
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append("CategoryAndMetricsForSources");
+        sb.append("{categoryAndMetrics=").append(categoryAndMetrics);
+        sb.append(", sources=").append(sources);
+        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 CategoryAndMetricsForSources that = (CategoryAndMetricsForSources) o;
+
+        if (categoryAndMetrics != null ? !categoryAndMetrics.equals(that.categoryAndMetrics) : that.categoryAndMetrics != null) {
+            return false;
+        }
+        if (sources != null ? !sources.equals(that.sources) : that.sources != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = categoryAndMetrics != null ? categoryAndMetrics.hashCode() : 0;
+        result = 31 * result + (sources != null ? sources.hashCode() : 0);
+        return result;
+    }
+
+    @Override
+    public int compareTo(final CategoryAndMetricsForSources o) {
+        return categoryAndMetrics.compareTo(o.getCategoryAndMetrics());
+    }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryIdAndMetric.java b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryIdAndMetric.java
new file mode 100644
index 0000000..30d58bb
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryIdAndMetric.java
@@ -0,0 +1,68 @@
+/*
+ * 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.categories;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class CategoryIdAndMetric {
+
+    private final int eventCategoryId;
+    private final String metric;
+
+    public CategoryIdAndMetric(final int eventCategoryId, final String metric) {
+        this.eventCategoryId = eventCategoryId;
+        this.metric = metric;
+    }
+
+    public int getEventCategoryId() {
+        return eventCategoryId;
+    }
+
+    public String getMetric() {
+        return metric;
+    }
+
+    @Override
+    public boolean equals(final Object other) {
+        if (other == null || !(other instanceof CategoryIdAndMetric)) {
+            return false;
+        } else {
+            final CategoryIdAndMetric typedOther = (CategoryIdAndMetric) other;
+            return eventCategoryId == typedOther.getEventCategoryId() && metric.equals(typedOther.getMetric());
+        }
+    }
+
+    @Override
+    public int hashCode() {
+        return eventCategoryId ^ metric.hashCode();
+    }
+
+    @Override
+    public String toString() {
+        return String.format("EventCategoryIdAndMetric(eventCategoryId %d, metric %s)", eventCategoryId, metric);
+    }
+
+    public static List<String> extractMetrics(final Collection<CategoryIdAndMetric> categoryIdsAndMetrics) {
+        final List<String> metrics = new ArrayList<String>();
+        for (final CategoryIdAndMetric categoryIdAndMetric : categoryIdsAndMetrics) {
+            metrics.add(categoryIdAndMetric.getMetric());
+        }
+        return metrics;
+    }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryIdAndMetricBinder.java b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryIdAndMetricBinder.java
new file mode 100644
index 0000000..1c75f83
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryIdAndMetricBinder.java
@@ -0,0 +1,48 @@
+/*
+ * 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.categories;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.skife.jdbi.v2.SQLStatement;
+import org.skife.jdbi.v2.sqlobject.Binder;
+import org.skife.jdbi.v2.sqlobject.BinderFactory;
+import org.skife.jdbi.v2.sqlobject.BindingAnnotation;
+
+import com.ning.billing.usage.timeline.categories.CategoryIdAndMetricBinder.CategoryIdAndMetricBinderFactory;
+
+@BindingAnnotation(CategoryIdAndMetricBinderFactory.class)
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.PARAMETER})
+public @interface CategoryIdAndMetricBinder {
+
+    public static class CategoryIdAndMetricBinderFactory implements BinderFactory {
+
+        public Binder build(final Annotation annotation) {
+            return new Binder<CategoryIdAndMetricBinder, CategoryIdAndMetric>() {
+                public void bind(final SQLStatement query, final CategoryIdAndMetricBinder binder, final CategoryIdAndMetric categoryAndKind) {
+                    query.bind("eventCategoryId", categoryAndKind.getEventCategoryId())
+                         .bind("metric", categoryAndKind.getMetric());
+                }
+            };
+        }
+    }
+}
diff --git a/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryIdAndMetricMapper.java b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryIdAndMetricMapper.java
new file mode 100644
index 0000000..2ca01a2
--- /dev/null
+++ b/usage/src/main/java/com/ning/billing/usage/timeline/categories/CategoryIdAndMetricMapper.java
@@ -0,0 +1,35 @@
+/*
+ * 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.categories;
+
+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.categories.CategoryIdAndMetric;
+
+public class CategoryIdAndMetricMapper implements ResultSetMapper<CategoryIdAndMetric> {
+
+    @Override
+    public CategoryIdAndMetric map(final int index, final ResultSet rs, final StatementContext ctx) throws SQLException {
+        final int eventCategoryId = rs.getInt("event_category_id");
+        final String metric = rs.getString("sample_kind");
+        return new CategoryIdAndMetric(eventCategoryId, metric);
+    }
+}