/*
 * Copyright 2014, The OpenNMS Group
 * 
 * Licensed 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 org.opennms.newts.cassandra.search;


import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;

import com.codahale.metrics.Meter;
import com.google.common.base.Optional;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;


public class ResourceMetadata implements Serializable {

    private static final long serialVersionUID = 2103401685689076369L;

    private final Set<String> m_metrics = Sets.newConcurrentHashSet();
    private final Map<String, String> m_attributes = Maps.newConcurrentMap();
    private final transient Optional<Meter> m_metricReqs;
    private final transient Optional<Meter> m_attributeReqs;
    private final transient Optional<Meter> m_metricMisses;
    private final transient Optional<Meter> m_attributeMisses;

    public ResourceMetadata(Meter metricReqs, Meter attributeReqs, Meter metricMisses, Meter attributeMisses) {
        m_metricReqs = Optional.of(checkNotNull(metricReqs, "metricsReqs argument"));
        m_attributeReqs = Optional.of(checkNotNull(attributeReqs, "attributesReqs argument"));
        m_metricMisses = Optional.of(checkNotNull(metricMisses, "metricsMisses argument"));
        m_attributeMisses = Optional.of(checkNotNull(attributeMisses, "attributesMisses argument"));
    }

    public ResourceMetadata() {
        m_metricReqs = Optional.absent();
        m_attributeReqs = Optional.absent();
        m_metricMisses = Optional.absent();
        m_attributeMisses = Optional.absent();
    }

    public boolean containsMetric(String metric) {
        if (m_metricReqs.isPresent()) m_metricReqs.get().mark();
        boolean contains = m_metrics.contains(metric);
        if ((!contains) && m_metricMisses.isPresent()) m_metricMisses.get().mark(); 
        return contains;
    }

    public ResourceMetadata putMetric(String metric) {
        m_metrics.add(metric);
        return this;
    }

    public boolean containsAttribute(String key, String value) {
        if (m_attributeReqs.isPresent()) m_attributeReqs.get().mark();
        boolean contains = m_attributes.containsKey(key) && m_attributes.get(key).equals(value);
        if ((!contains) && m_attributeMisses.isPresent()) m_attributeMisses.get().mark();
        return contains;
    }

    public ResourceMetadata putAttribute(String key, String value) {
        m_attributes.put(key, value);
        return this;
    }

    /**
     * Merges the metrics and attributes from the given instance, to the current instance.
     *
     * @param other a {@link ResourceMetadata} object
     * @return true if the meta-data was modified as a result of the merge, false otherwise
     */
    public boolean merge(ResourceMetadata other) {
        boolean modified = m_metrics.addAll(other.m_metrics);
        if (!modified) {
            modified = !m_attributes.equals(other.m_attributes);
        }
        m_attributes.putAll(other.m_attributes);
        return modified;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((m_attributes == null) ? 0 : m_attributes.hashCode());
        result = prime * result + ((m_metrics == null) ? 0 : m_metrics.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        ResourceMetadata other = (ResourceMetadata) obj;
        if (m_attributes == null) {
            if (other.m_attributes != null) return false;
        }
        else if (!m_attributes.equals(other.m_attributes)) return false;
        if (m_metrics == null) {
            if (other.m_metrics != null) return false;
        }
        else if (!m_metrics.equals(other.m_metrics)) return false;
        return true;
    }

}

@@ -652,74 +652,8 @@
 ;%0A%0A%0A
-import static com.google.common.base.Preconditions.checkNotNull;%0A%0A
 impo
@@ -761,48 +761,8 @@
 er;%0A
-import com.google.common.base.Optional;%0A
 impo
@@ -836,17 +836,16 @@
 .Sets;%0A%0A
-%0A
 public c
@@ -1138,39 +1138,29 @@
 l transient 
-Optional%3C
 Meter
-%3E
  m_metricReq
@@ -1186,39 +1186,29 @@
 l transient 
-Optional%3C
 Meter
-%3E
  m_attribute
@@ -1241,31 +1241,21 @@
 ansient 
-Optional%3C
 Meter
-%3E
  m_metri
@@ -1295,23 +1295,13 @@
 ent 
-Optional%3C
 Meter
-%3E
  m_a
@@ -1456,357 +1456,143 @@
 s = 
-Optional.of(checkNotNull(metricReqs, %22metricsReqs argument%22));%0A        m_attributeReqs = Optional.of(checkNotNull(attributeReqs, %22attributesReqs argument%22));%0A        m_metricMisses = Optional.of(checkNotNull(metricMisses, %22metricsMisses argument%22));%0A        m_attributeMisses = Optional.of(checkNotNull(attributeMisses, %22attributesMisses argument%22))
+metricReqs;%0A        m_attributeReqs = attributeReqs;%0A        m_metricMisses = metricMisses;%0A        m_attributeMisses = attributeMisses
 ;%0A  
@@ -1651,33 +1651,20 @@
 cReqs = 
-Optional.absent()
+null
 ;%0A      
@@ -1683,33 +1683,20 @@
 eReqs = 
-Optional.absent()
+null
 ;%0A      
@@ -1714,33 +1714,20 @@
 isses = 
-Optional.absent()
+null
 ;%0A      
@@ -1752,25 +1752,12 @@
 s = 
-Optional.absent()
+null
 ;%0A  
@@ -1832,36 +1832,32 @@
 m_metricReqs
-.isPresent()
+ != null
 ) m_metricRe
@@ -1855,30 +1855,24 @@
 _metricReqs.
-get().
 mark();%0A    
@@ -1959,36 +1959,32 @@
 metricMisses
-.isPresent()
+ != null
 ) m_metricMi
@@ -1992,22 +1992,15 @@
 ses.
-get().
 mark();
- 
 %0A   
@@ -2234,28 +2234,24 @@
 buteReqs
-.isPresent()
+ != null
 ) m_attr
@@ -2260,22 +2260,16 @@
 uteReqs.
-get().
 mark();%0A
@@ -2413,20 +2413,16 @@
 sses
-.isPresent()
+ != null
 ) m_
@@ -2441,14 +2441,8 @@
 ses.
-get().
 mark
@@ -4079,11 +4079,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Use nulls instead of Optionals to avoid NPEs after deserialization.