Java tutorial
/** * Copyright 2014-2015 SHAF-WORK * * 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.shaf.core._helper_; import java.util.HashMap; import java.util.Map; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.shaf.core.util.ClassUtils; import com.google.common.base.Objects; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; /** * The dummy data generator. * * @author Mykola Galushka */ public class DummyData<KEY, VALUE> { public static final String[][] MATRIX_NUMBER_STRING = { { "0", "a" }, { "1", "b" }, { "2", "c" } }; private Map<KEY, VALUE> data; private Class<? extends KEY> kCls; private Class<? extends VALUE> vCls; public DummyData(final Class<? extends KEY> kCls, final Class<? extends VALUE> vCls) { this.data = new HashMap<>(); this.kCls = kCls; this.vCls = vCls; } @Override @SuppressWarnings("rawtypes") public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof DummyData)) { return false; } DummyData other = (DummyData) obj; if (this.data.size() == other.data.size()) { for (Map.Entry<KEY, VALUE> entry : this.data.entrySet()) { if (entry.getValue().equals(other.data.get(entry.getKey()))) { continue; } else { return false; } } return true; } return false; } @Override public int hashCode() { return Objects.hashCode(this.data, this.kCls, this.vCls); } @SuppressWarnings("rawtypes") public final DummyData add(final Object key, final Object value) { this.data.put(this.<KEY>cast(this.kCls, key), this.<VALUE>cast(this.vCls, value)); return this; } @SuppressWarnings({ "rawtypes" }) public final DummyData generateFor(final String[][] matrix) { for (int i = 0; i < matrix.length; i++) { this.add(matrix[i][0], matrix[i][1]); } return this; } @SuppressWarnings({ "rawtypes" }) public final DummyData generateFor(final Map<KEY, VALUE> map) { for (Map.Entry<KEY, VALUE> entry : map.entrySet()) { this.data.put(entry.getKey(), entry.getValue()); } return this; } @SuppressWarnings({ "rawtypes" }) public final DummyData generateFor(final Multimap<KEY, VALUE> multimap) { for (Map.Entry<KEY, VALUE> entry : multimap.entries()) { this.data.put(entry.getKey(), entry.getValue()); } return this; } @SuppressWarnings("unchecked") private final <ELEM> ELEM cast(Class<?> cls, final Object obj) { if (cls == obj.getClass()) { return (ELEM) obj; } if (ClassUtils.isInteger(cls)) { return (ELEM) new Integer((String) obj); } else if (ClassUtils.isLong(cls)) { return (ELEM) new Long((String) obj); } if (cls == IntWritable.class) { return (ELEM) new IntWritable(Integer.parseInt((String) obj)); } else if (cls == LongWritable.class) { return (ELEM) new LongWritable(Long.parseLong((String) obj)); } else if (cls == Text.class) { return (ELEM) new Text((String) obj); } if (ClassUtils.isShort(cls)) { return (ELEM) obj; } return null; } public final Map<KEY, VALUE> asMap() { return this.data; } public final Multimap<KEY, VALUE> asMultimap() { Multimap<KEY, VALUE> multimap = HashMultimap.create(); for (Map.Entry<KEY, VALUE> entry : this.data.entrySet()) { multimap.put(entry.getKey(), entry.getValue()); } return multimap; } /** * Returns the {@code DummyData} as a string. */ @Override public String toString() { return Objects.toStringHelper(this).add("kCls", this.kCls).add("vCls", this.vCls).add("data", this.data) .toString(); } }