Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoberto E. Escobar2013-12-02 15:04:11 -0500
committerRoberto E. Escobar2013-12-03 19:05:37 -0500
commit5c05723b76b315d0710403a649da6afdd84ed3f9 (patch)
tree45eb0eab5ddc0015ad539b64fa34c7e7bf4b950f /plugins/org.eclipse.osee.framework.jdk.core
parent94a1a53681dcdfddf07d11e37fcb161642cc6da6 (diff)
downloadorg.eclipse.osee-5c05723b76b315d0710403a649da6afdd84ed3f9.tar.gz
org.eclipse.osee-5c05723b76b315d0710403a649da6afdd84ed3f9.tar.xz
org.eclipse.osee-5c05723b76b315d0710403a649da6afdd84ed3f9.zip
refactor: Move ResultSet to jdk core
Diffstat (limited to 'plugins/org.eclipse.osee.framework.jdk.core')
-rw-r--r--plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ItemDoesNotExist.java37
-rw-r--r--plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/MultipleItemsExist.java37
-rw-r--r--plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSet.java30
-rw-r--r--plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetIterable.java99
-rw-r--r--plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSetList.java85
-rw-r--r--plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/type/ResultSets.java63
6 files changed, 351 insertions, 0 deletions
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 0000000000..36e9c1c532
--- /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 0000000000..b0b15b375d
--- /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 0000000000..07a5507715
--- /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<T> extends Iterable<T> {
+
+ 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 0000000000..fceb6c7d83
--- /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<T> implements ResultSet<T> {
+
+ private final Iterable<T> data;
+
+ protected ResultSetIterable(Iterable<T> 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<T> getData() {
+ return data;
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ return getData().iterator();
+ }
+
+ @Override
+ public int size() {
+ Iterable<T> 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<T> 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 0000000000..975ed1c258
--- /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<T> implements ResultSet<T> {
+
+ private final List<T> data;
+
+ protected ResultSetList() {
+ super();
+ this.data = Collections.emptyList();
+ }
+
+ protected ResultSetList(List<T> data) {
+ super();
+ this.data = data;
+ }
+
+ @Override
+ public T getOneOrNull() {
+ List<T> result = getList();
+ return result.isEmpty() ? null : result.iterator().next();
+ }
+
+ @Override
+ public T getAtMostOneOrNull() throws OseeCoreException {
+ List<T> 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<T> getList() {
+ return data;
+ }
+
+ @Override
+ public Iterator<T> 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 0000000000..1698a34f9f
--- /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 <T> ResultSet<T> singleton(T item) {
+ ResultSet<T> toReturn;
+ if (item == null) {
+ toReturn = emptyResultSet();
+ } else {
+ toReturn = new ResultSetList<T>(Collections.singletonList(item));
+ }
+ return toReturn;
+ }
+
+ public static <T> ResultSet<T> newResultSet(List<T> list) {
+ ResultSet<T> toReturn;
+ if (list.isEmpty()) {
+ toReturn = emptyResultSet();
+ } else {
+ toReturn = new ResultSetList<T>(list);
+ }
+ return toReturn;
+ }
+
+ public static <T> ResultSet<T> newResultSet(Iterable<T> iterable) {
+ ResultSet<T> toReturn;
+ if (iterable == null || !iterable.iterator().hasNext()) {
+ toReturn = emptyResultSet();
+ } else {
+ toReturn = new ResultSetIterable<T>(iterable);
+ }
+ return toReturn;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T> ResultSet<T> emptyResultSet() {
+ return EMPTY_RESULT_SET;
+ }
+
+}

Back to the top