package com.dianping.cat.storage;

import java.io.IOException;
import java.util.Date;

import org.unidal.lookup.ContainerHolder;

public class DefaultBucketManager extends ContainerHolder implements BucketManager {

	@Override
	public void closeBucket(Bucket<?> bucket) {
		try {
			bucket.close();
		} catch (Exception e) {
			// ignore it
		}
		release(bucket);
	}

	protected Bucket<?> createBucket(Class<?> type, Date timestamp, String name, String namespace) throws IOException {
		Bucket<?> bucket = lookup(Bucket.class, type.getName() + "-" + namespace);

		bucket.initialize(type, name, timestamp);
		return bucket;
	}

	@SuppressWarnings("unchecked")
	protected <T> Bucket<T> getBucket(Class<T> type, long timestamp, String name, String namespace) throws IOException {
		Date date = new Date(timestamp);
		Bucket<?> bucket = createBucket(type, date, name, namespace);

		return (Bucket<T>) bucket;
	}

	@Override
	public Bucket<String> getReportBucket(long timestamp, String name) throws IOException {
		return getBucket(String.class, timestamp, name, "report");
	}

	public static class Entry {
		private String m_namespace;

		private String m_path;

		private Class<?> m_type;

		public Entry(Class<?> type, String path, String namespace) {
			m_type = type;
			m_path = path;
			m_namespace = namespace;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj instanceof Entry) {
				Entry e = (Entry) obj;

				return e.m_type == m_type && e.m_path.equals(m_path) && e.m_namespace.equals(m_namespace);
			}

			return false;
		}

		@Override
		public int hashCode() {
			int hashcode = m_type.hashCode();

			hashcode = hashcode * 31 + m_path.hashCode();
			return hashcode;
		}

		@Override
		public String toString() {
			return String.format("Entry[type=%s,path=%s]", m_type, m_path);
		}
	}
}

@@ -1073,749 +1073,6 @@
 %09%7D%0A%0A
-%09public static class Entry %7B%0A%09%09private String m_namespace;%0A%0A%09%09private String m_path;%0A%0A%09%09private Class%3C?%3E m_type;%0A%0A%09%09public Entry(Class%3C?%3E type, String path, String namespace) %7B%0A%09%09%09m_type = type;%0A%09%09%09m_path = path;%0A%09%09%09m_namespace = namespace;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public boolean equals(Object obj) %7B%0A%09%09%09if (obj instanceof Entry) %7B%0A%09%09%09%09Entry e = (Entry) obj;%0A%0A%09%09%09%09return e.m_type == m_type && e.m_path.equals(m_path) && e.m_namespace.equals(m_namespace);%0A%09%09%09%7D%0A%0A%09%09%09return false;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public int hashCode() %7B%0A%09%09%09int hashcode = m_type.hashCode();%0A%0A%09%09%09hashcode = hashcode * 31 + m_path.hashCode();%0A%09%09%09return hashcode;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public String toString() %7B%0A%09%09%09return String.format(%22Entry%5Btype=%25s,path=%25s%5D%22, m_type, m_path);%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

remove unused code