From 5c05723b76b315d0710403a649da6afdd84ed3f9 Mon Sep 17 00:00:00 2001 From: Roberto E. Escobar Date: Mon, 2 Dec 2013 13:04:11 -0700 Subject: refactor: Move ResultSet to jdk core Change-Id: I0e3c8728daa6bb83f59fd305d11ca126ba414d84 --- .../framework/jdk/core/type/ItemDoesNotExist.java | 37 ++++++++ .../jdk/core/type/MultipleItemsExist.java | 37 ++++++++ .../osee/framework/jdk/core/type/ResultSet.java | 30 +++++++ .../framework/jdk/core/type/ResultSetIterable.java | 99 ++++++++++++++++++++++ .../framework/jdk/core/type/ResultSetList.java | 85 +++++++++++++++++++ .../osee/framework/jdk/core/type/ResultSets.java | 63 ++++++++++++++ 6 files changed, 351 insertions(+) create mode 100644 plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ItemDoesNotExist.java create mode 100644 plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/MultipleItemsExist.java create mode 100644 plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSet.java create mode 100644 plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetIterable.java create mode 100644 plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetList.java create mode 100644 plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSets.java (limited to 'plugins/org.eclipse.osee.framework.jdk.core') diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ItemDoesNotExist.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ItemDoesNotExist.java new file mode 100644 index 00000000000..36e9c1c5323 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ItemDoesNotExist.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + + + +/** + * @author Roberto E. Escobar + */ +public class ItemDoesNotExist extends OseeCoreException { + + private static final long serialVersionUID = 1L; + + public ItemDoesNotExist(String message, Object... args) { + super(message, args); + } + + public ItemDoesNotExist(String message, Throwable cause) { + super(message, cause); + } + + public ItemDoesNotExist(Throwable cause, String message, Object... args) { + super(cause, message, args); + } + + public ItemDoesNotExist(Throwable cause) { + super(cause); + } +} \ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/MultipleItemsExist.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/MultipleItemsExist.java new file mode 100644 index 00000000000..b0b15b375d9 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/MultipleItemsExist.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + + + +/** + * @author Roberto E. Escobar + */ +public class MultipleItemsExist extends OseeCoreException { + + private static final long serialVersionUID = 1L; + + public MultipleItemsExist(String message, Object... args) { + super(message, args); + } + + public MultipleItemsExist(String message, Throwable cause) { + super(message, cause); + } + + public MultipleItemsExist(Throwable cause, String message, Object... args) { + super(cause, message, args); + } + + public MultipleItemsExist(Throwable cause) { + super(cause); + } +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSet.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSet.java new file mode 100644 index 00000000000..07a55077158 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSet.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + + +/** + * @author Ryan D. Brooks + * @author Roberto E. Escobar + */ +public interface ResultSet extends Iterable { + + T getOneOrNull() throws OseeCoreException; + + T getExactlyOne() throws OseeCoreException; + + T getAtMostOneOrNull() throws OseeCoreException; + + int size(); + + boolean isEmpty(); + +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetIterable.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetIterable.java new file mode 100644 index 00000000000..fceb6c7d830 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetIterable.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.util.Collection; +import java.util.Iterator; + +/** + * @author Roberto E. Escobar + */ +public class ResultSetIterable implements ResultSet { + + private final Iterable data; + + protected ResultSetIterable(Iterable iterable) { + super(); + this.data = iterable; + } + + @Override + public T getOneOrNull() { + T result = null; + int size = size(); + if (size > 0) { + result = iterator().next(); + } + return result; + } + + @Override + public T getAtMostOneOrNull() throws OseeCoreException { + T result = null; + int size = size(); + if (size > 1) { + throw createManyExistException(size); + } else if (size == 1) { + result = iterator().next(); + } + return result; + } + + @Override + public T getExactlyOne() throws OseeCoreException { + T result = getAtMostOneOrNull(); + if (result == null) { + throw createDoesNotExistException(); + } + return result; + } + + private Iterable getData() { + return data; + } + + @Override + public Iterator iterator() { + return getData().iterator(); + } + + @Override + public int size() { + Iterable it = getData(); + int count = 0; + if (it instanceof Collection) { + count = ((Collection) it).size(); + } else { + count = 0; + Iterator iterator = it.iterator(); + while (iterator.hasNext()) { + iterator.next(); + count++; + } + } + return count; + } + + @Override + public boolean isEmpty() { + Iterable it = getData(); + return it == null || !it.iterator().hasNext(); + } + + protected OseeCoreException createManyExistException(int count) { + return new MultipleItemsExist("Multiple items found - total [%s]", count); + } + + protected OseeCoreException createDoesNotExistException() { + return new ItemDoesNotExist("No item found"); + } + +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetList.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetList.java new file mode 100644 index 00000000000..975ed1c2586 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetList.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +/** + * @author Roberto E. Escobar + */ +public class ResultSetList implements ResultSet { + + private final List data; + + protected ResultSetList() { + super(); + this.data = Collections.emptyList(); + } + + protected ResultSetList(List data) { + super(); + this.data = data; + } + + @Override + public T getOneOrNull() { + List result = getList(); + return result.isEmpty() ? null : result.iterator().next(); + } + + @Override + public T getAtMostOneOrNull() throws OseeCoreException { + List result = getList(); + if (result.size() > 1) { + throw createManyExistException(result.size()); + } + return result.isEmpty() ? null : result.iterator().next(); + } + + @Override + public T getExactlyOne() throws OseeCoreException { + T result = getAtMostOneOrNull(); + if (result == null) { + throw createDoesNotExistException(); + } + return result; + } + + private List getList() { + return data; + } + + @Override + public Iterator iterator() { + return getList().iterator(); + } + + protected OseeCoreException createManyExistException(int count) { + return new MultipleItemsExist("Multiple items found - total [%s]", count); + } + + protected OseeCoreException createDoesNotExistException() { + return new ItemDoesNotExist("No item found"); + } + + @Override + public int size() { + return data.size(); + } + + @Override + public boolean isEmpty() { + return data.isEmpty(); + } + +} diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSets.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSets.java new file mode 100644 index 00000000000..1698a34f9f1 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSets.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2013 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.type; + +import java.util.Collections; +import java.util.List; + +/** + * @author Roberto E. Escobar + */ +public final class ResultSets { + + @SuppressWarnings({"rawtypes", "unchecked"}) + private static final ResultSet EMPTY_RESULT_SET = new ResultSetList(Collections.emptyList()); + + private ResultSets() { + // Utility + } + + public static ResultSet singleton(T item) { + ResultSet toReturn; + if (item == null) { + toReturn = emptyResultSet(); + } else { + toReturn = new ResultSetList(Collections.singletonList(item)); + } + return toReturn; + } + + public static ResultSet newResultSet(List list) { + ResultSet toReturn; + if (list.isEmpty()) { + toReturn = emptyResultSet(); + } else { + toReturn = new ResultSetList(list); + } + return toReturn; + } + + public static ResultSet newResultSet(Iterable iterable) { + ResultSet toReturn; + if (iterable == null || !iterable.iterator().hasNext()) { + toReturn = emptyResultSet(); + } else { + toReturn = new ResultSetIterable(iterable); + } + return toReturn; + } + + @SuppressWarnings("unchecked") + public static ResultSet emptyResultSet() { + return EMPTY_RESULT_SET; + } + +} -- cgit v1.2.3