[ldap-sdk-commits] SF.net SVN: ldap-sdk:[1588] trunk
A Java-based LDAP API
Brought to you by:
dirmgr,
kennethleo
From: <di...@us...> - 2023-02-07 23:12:51
|
Revision: 1588 http://sourceforge.net/p/ldap-sdk/code/1588 Author: dirmgr Date: 2023-02-07 23:12:49 +0000 (Tue, 07 Feb 2023) Log Message: ----------- Add an ObjectTrio class Added a new ObjectTrio utility class that can be useful in cases where only a single object is allowed but three typed objects are needed (e.g., when you want to return three typed items from a method). Modified Paths: -------------- trunk/docs/release-notes.html Added Paths: ----------- trunk/src/com/unboundid/util/ObjectTrio.java trunk/tests/unit/src/com/unboundid/util/ObjectTrioTestCase.java Modified: trunk/docs/release-notes.html =================================================================== --- trunk/docs/release-notes.html 2023-01-27 23:48:04 UTC (rev 1587) +++ trunk/docs/release-notes.html 2023-02-07 23:12:49 UTC (rev 1588) @@ -60,6 +60,13 @@ </li> <li> + Added a new ObjectTrio utility class that can be useful in cases where only a + single object is allowed but three typed objects are needed (e.g., when you want + to return three typed items from a method). + <br><br> + </li> + + <li> Updated documentation to include the latest versions of draft-howard-gssapi-aead, draft-ietf-kitten-scram-2fa, draft-melnikov-scram-bis, and draft-reitzenstein-kitten-opaque in the set of LDAP-related specifications. Added: trunk/src/com/unboundid/util/ObjectTrio.java =================================================================== --- trunk/src/com/unboundid/util/ObjectTrio.java (rev 0) +++ trunk/src/com/unboundid/util/ObjectTrio.java 2023-02-07 23:12:49 UTC (rev 1588) @@ -0,0 +1,271 @@ +/* + * Copyright 2023 Ping Identity Corporation + * All Rights Reserved. + */ +/* + * Copyright 2023 Ping Identity Corporation + * + * 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. + */ +/* + * Copyright (C) 2023 Ping Identity Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License (GPLv2 only) + * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses>. + */ +package com.unboundid.util; + + + +import java.io.Serializable; + + + +/** + * This class provides a typed trio of objects. It may be used whenever three + * objects are required but only one is allowed (e.g., returning three values + * from a method). + * + * @param <F> The type of the first object. + * @param <S> The type of the second object. + * @param <T> The type of the third object. + */ +@NotMutable() +@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) +public final class ObjectTrio<F,S,T> + implements Serializable +{ + /** + * The serial version UID for this serializable class. + */ + private static final long serialVersionUID = 1205337843902801247L; + + + + // The first object in this trio. + @Nullable private final F first; + + // The second object in this trio. + @Nullable private final S second; + + // The third object in this trio. + @Nullable private final T third; + + + + /** + * Creates a new object trio with the provided elements. + * + * @param first The first object in this trio. + * @param second The second object in this trio. + * @param third The third object in this trio. + */ + public ObjectTrio(@Nullable final F first, + @Nullable final S second, + @Nullable final T third) + { + this.first = first; + this.second = second; + this.third = third; + } + + + + /** + * Retrieves the first object in this trio. + * + * @return The first object in this trio. + */ + @Nullable() + public F getFirst() + { + return first; + } + + + + /** + * Retrieves the second object in this trio. + * + * @return The second object in this trio. + */ + @Nullable() + public S getSecond() + { + return second; + } + + + + /** + * Retrieves the third object in this trio. + * + * @return The third object in this trio. + */ + @Nullable() + public T getThird() + { + return third; + } + + + + /** + * Retrieves a hash code for this object trio. + * + * @return A hash code for this object trio. + */ + @Override() + public int hashCode() + { + int h = 0; + + if (first != null) + { + h += first.hashCode(); + } + + if (second != null) + { + h += second.hashCode(); + } + + if (third != null) + { + h += third.hashCode(); + } + + return h; + } + + + + /** + * Indicates whether the provided object is equal to this object trio. + * + * @param o The object for which to make the determination. + * + * @return {@code true} if the provided object is equal to this object trio, + * or {@code false} if not. + */ + @Override() + public boolean equals(@Nullable final Object o) + { + if (o == null) + { + return false; + } + + if (o == this) + { + return true; + } + + if (o instanceof ObjectTrio) + { + final ObjectTrio<?,?,?> t = (ObjectTrio<?,?,?>) o; + if (first == null) + { + if (t.first != null) + { + return false; + } + } + else + { + if (! first.equals(t.first)) + { + return false; + } + } + + if (second == null) + { + if (t.second != null) + { + return false; + } + } + else + { + if (! second.equals(t.second)) + { + return false; + } + } + + if (third == null) + { + if (t.third != null) + { + return false; + } + } + else + { + if (! third.equals(t.third)) + { + return false; + } + } + + return true; + } + + return false; + } + + + + /** + * Retrieves a string representation of this object trio. + * + * @return A string representation of this object trio. + */ + @Override() + @NotNull() + public String toString() + { + final StringBuilder buffer = new StringBuilder(); + toString(buffer); + return buffer.toString(); + } + + + + /** + * Appends a string representation of this object trio to the provided buffer. + * + * @param buffer The buffer to which the information should be appended. + */ + public void toString(@NotNull final StringBuilder buffer) + { + buffer.append("ObjectTrio(first="); + buffer.append(String.valueOf(first)); + buffer.append(", second="); + buffer.append(String.valueOf(second)); + buffer.append(", third="); + buffer.append(String.valueOf(third)); + buffer.append(')'); + } +} Added: trunk/tests/unit/src/com/unboundid/util/ObjectTrioTestCase.java =================================================================== --- trunk/tests/unit/src/com/unboundid/util/ObjectTrioTestCase.java (rev 0) +++ trunk/tests/unit/src/com/unboundid/util/ObjectTrioTestCase.java 2023-02-07 23:12:49 UTC (rev 1588) @@ -0,0 +1,279 @@ +/* + * Copyright 2011-2023 Ping Identity Corporation + * All Rights Reserved. + */ +/* + * Copyright 2011-2023 Ping Identity Corporation + * + * 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. + */ +/* + * Copyright (C) 2011-2023 Ping Identity Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License (GPLv2 only) + * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <http://www.gnu.org/licenses>. + */ +package com.unboundid.util; + + + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import com.unboundid.ldap.sdk.LDAPSDKTestCase; + + + +/** + * This class provides a set of test cases for the {@code ObjectTrio} class. + */ +public final class ObjectTrioTestCase + extends LDAPSDKTestCase +{ + /** + * Tests the behavior for an object trio in which all three elements are + * non-{@code null}. + * + * @throws Exception If an unexpected problem occurs. + */ + @Test() + public void testAllNonNull() + throws Exception + { + final ObjectTrio<String,Boolean,Integer> trio = + new ObjectTrio<>("foo", true, 1234); + assertNotNull(trio); + + assertNotNull(trio.getFirst()); + assertEquals(trio.getFirst(), "foo"); + + assertNotNull(trio.getSecond()); + assertEquals(trio.getSecond(), Boolean.TRUE); + + assertNotNull(trio.getThird()); + assertEquals(trio.getThird(), Integer.valueOf(1234)); + + assertEquals(trio.hashCode(), + ("foo".hashCode() + Boolean.TRUE.hashCode() + + Integer.valueOf(1234).hashCode())); + + assertTrue(trio.equals(new ObjectTrio<>("foo", true, 1234))); + + assertNotNull(trio.toString()); + assertEquals(trio.toString(), "ObjectTrio(first=foo, second=true, " + + "third=1234)"); + } + + + + /** + * Tests the behavior for an object trio in which all three elements are + * {@code null}. + * + * @throws Exception If an unexpected problem occurs. + */ + @Test() + public void testAllNull() + throws Exception + { + final ObjectTrio<String,Boolean,Integer> trio = + new ObjectTrio<>(null, null, null); + assertNotNull(trio); + + assertNull(trio.getFirst()); + assertNull(trio.getSecond()); + assertNull(trio.getThird()); + + assertEquals(trio.hashCode(), 0); + + assertTrue(trio.equals(new ObjectTrio<>(null, null, null))); + + assertNotNull(trio.toString()); + assertEquals(trio.toString(), "ObjectTrio(first=null, second=null, " + + "third=null)"); + } + + + + /** + * Tests the behavior for an object trio in which there are a mix of + * {@code null} and non-{@code null} elements. + * + * @throws Exception If an unexpected problem occurs. + */ + @Test() + public void testSomeNull() + throws Exception + { + final ObjectTrio<String,String,String> trio = + new ObjectTrio<>("foo", null, "bar"); + assertNotNull(trio); + + assertNotNull(trio.getFirst()); + assertEquals(trio.getFirst(), "foo"); + + assertNull(trio.getSecond()); + + assertNotNull(trio.getThird()); + assertEquals(trio.getThird(), "bar"); + + assertEquals(trio.hashCode(), + ("foo".hashCode() + "bar".hashCode())); + + assertTrue(trio.equals(new ObjectTrio<>("foo", null, "bar"))); + + assertNotNull(trio.toString()); + assertEquals(trio.toString(), "ObjectTrio(first=foo, second=null, " + + "third=bar)"); + } + + + + /** + * Tests the behavior of the {@code equals} method. + * + * @param o The object to use for testing. + * @param m Indicates whether to expect the provided object to match the + * given test object. + * + * @throws Exception If an unexpected problem occurs. + */ + @Test(dataProvider="equalsData") + public void testEquals(final Object o, final boolean m) + throws Exception + { + final ObjectTrio<String,String,String> t = + new ObjectTrio<>("foo", "bar", "baz"); + + assertTrue(t.equals(t)); + + assertEquals(t.equals(o), m); + + if (o != null) + { + assertEquals(o.equals(t), m); + } + + assertFalse(t.equals(null)); + + assertFalse(t.equals("foo")); + + assertTrue(t.equals(new ObjectTrio<>("foo", "bar", "baz"))); + + assertTrue(t.equals(new ObjectTrio<Object,Object,Object>( + "foo", "bar", "baz"))); + + assertFalse(t.equals(new ObjectTrio<>(1, 2, 3))); + + assertFalse(t.equals(new ObjectTrio<>("different", "bar", "baz"))); + + assertFalse(t.equals(new ObjectTrio<>("foo", "different", "baz"))); + + assertFalse(t.equals(new ObjectTrio<>("foo", "bar", "different"))); + + assertFalse(t.equals(new ObjectTrio<>(null, null, null))); + } + + + + /** + * Provides a set of data for use in testing the {@code equals} method. + * + * @return A set of data for use in testing the {@code equals} method. + * + * @throws Exception If an unexpected problem occurs. + */ + @DataProvider(name="equalsData") + public Object[][] getEqualsData() + throws Exception + { + return new Object[][] + { + new Object[] + { + null, + false + }, + + new Object[] + { + "foo", + false + }, + + new Object[] + { + new ObjectTrio<>("foo", "bar", "baz"), + true + }, + + new Object[] + { + new ObjectTrio<Object,Object,Object>("foo", "bar", "baz"), + true + }, + + new Object[] + { + new ObjectTrio<>("foo", "foo", "baz"), + false + }, + + new Object[] + { + new ObjectTrio<>("foo", "bar", "foo"), + false + }, + + new Object[] + { + new ObjectTrio<>("foo", "bar", "bar"), + false + }, + + new Object[] + { + new ObjectTrio<>(null, "bar", "baz"), + false + }, + + new Object[] + { + new ObjectTrio<>("foo", null, "baz"), + false + }, + + new Object[] + { + new ObjectTrio<>("foo", "bar", null), + false + }, + + new Object[] + { + new ObjectTrio<>(1, 2, 3), + false + }, + }; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |