<commit_before>package tw.kewang.hbase.dao;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tw.kewang.hbase.annotations.Component;
import tw.kewang.hbase.annotations.Domain;
import tw.kewang.hbase.annotations.Table;
import tw.kewang.hbase.domain.AbstractDomain;

public abstract class AbstractDao {
	private static final Logger LOG = LoggerFactory
			.getLogger(AbstractDao.class);
	private static final HConnection CONNECTION = HConnectionHolder
			.getInstance().getHConnection();

	private final Table table = getTableAnnotation();
	private final Class<? extends AbstractDomain>[] domains = (table.domains());

	public AbstractDomain getByRowkey(String rowkey) {
		HTableInterface hTableInterface = null;

		try {
			hTableInterface = CONNECTION.getTable(table.name());

			Result result = hTableInterface.get(buildGet(rowkey));

			if (!result.isEmpty()) {
				return findDomain(rowkey);
			}
		} catch (Exception e) {
			LOG.error(Constants.EXCEPTION_PREFIX, e);
		} catch (Error e) {
			LOG.error(Constants.EXCEPTION_PREFIX, e);
		} finally {
			closeHTable(hTableInterface);
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	private Class<? extends AbstractDomain>[] sortDomains(
			Class<? extends AbstractDomain>[] srcDomains) {
		ArrayList<Class<? extends AbstractDomain>> destDomains = new ArrayList<Class<? extends AbstractDomain>>(
				Arrays.asList(srcDomains));

		Collections.sort(destDomains, new DomainComparator());

		return (Class<? extends AbstractDomain>[]) destDomains.toArray();
	}

	private AbstractDomain findDomain(String rowkey) throws Exception {
		for (Class<? extends AbstractDomain> domainClass : domains) {
			Domain domainAnnotation = domainClass.getAnnotation(Domain.class);
			ArrayList<DomainField> domainFields = buildDomain(domainClass,
					domainAnnotation.rowkey(), rowkey);

			if (domainFields == null) {
				continue;
			}

			for (DomainField domainField : domainFields) {
				boolean foundField = findField(domainClass, domainField);

				if (!foundField) {
					continue;
				}
			}

			AbstractDomain domain = domainClass.newInstance();
<commit_msg>Fix ClassCastException<commit_after>package tw.kewang.hbase.dao;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tw.kewang.hbase.annotations.Component;
import tw.kewang.hbase.annotations.Domain;
import tw.kewang.hbase.annotations.Table;
import tw.kewang.hbase.domain.AbstractDomain;

public abstract class AbstractDao {
	private static final Logger LOG = LoggerFactory
			.getLogger(AbstractDao.class);
	private static final HConnection CONNECTION = HConnectionHolder
			.getInstance().getHConnection();

	private final Table table = getTableAnnotation();
	private final Class<? extends AbstractDomain>[] domains = sortDomains(table
			.domains());

	public AbstractDomain getByRowkey(String rowkey) {
		HTableInterface hTableInterface = null;

		try {
			hTableInterface = CONNECTION.getTable(table.name());

			Result result = hTableInterface.get(buildGet(rowkey));

			if (!result.isEmpty()) {
				return findDomain(rowkey);
			}
		} catch (Exception e) {
			LOG.error(Constants.EXCEPTION_PREFIX, e);
		} catch (Error e) {
			LOG.error(Constants.EXCEPTION_PREFIX, e);
		} finally {
			closeHTable(hTableInterface);
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	private Class<? extends AbstractDomain>[] sortDomains(
			Class<? extends AbstractDomain>[] srcDomains) {
		ArrayList<Class<? extends AbstractDomain>> destDomains = new ArrayList<Class<? extends AbstractDomain>>(
				Arrays.asList(srcDomains));

		Collections.sort(destDomains, new DomainComparator());

		// XXX: http://stackoverflow.com/a/9293523/939212
		return (Class<? extends AbstractDomain>[]) destDomains
				.toArray(new Class[0]);
	}

	private AbstractDomain findDomain(String rowkey) throws Exception {
		for (Class<? extends AbstractDomain> domainClass : domains) {
			Domain domainAnnotation = domainClass.getAnnotation(Domain.class);
			ArrayList<DomainField> domainFields = buildDomain(domainClass,
					domainAnnotation.rowkey(), rowkey);

			if (domainFields == null) {
				continue;
			}

			for (DomainField domainField : domainFields) {
				boolean foundField = findField(domainClass, domainField);

				if (!foundField) {
					continue;
				}
			}

			AbstractDomain domain = domainClass.newInstance();

{'fraction_non_alphanumeric': 0.11161151360877228, 'fraction_numerical': 0.0035245741139612297, 'mean_word_length': 5.016489988221437, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1080227', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1489, 'n_words': 326}