-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use common interface for enum and string wrapper
- Loading branch information
Denis Anisimov
committed
May 5, 2021
1 parent
59498f2
commit 5e73864
Showing
5 changed files
with
210 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
flow-html-components/src/main/java/com/vaadin/flow/component/html/AnchorTargetValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.flow.component.html; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Represents <code>target</code> attribute values for an <code><a></code> | ||
* element. | ||
* | ||
* @author Vaadin Ltd | ||
* @since | ||
* | ||
* @see AnchorTarget | ||
* | ||
*/ | ||
public interface AnchorTargetValue { | ||
|
||
/** | ||
* Gets the string value representation. | ||
* | ||
* @return string value representation | ||
*/ | ||
String getValue(); | ||
|
||
/** | ||
* Gets an object instance wrapping the {@code value} string representation. | ||
* | ||
* @param value | ||
* the string value representation, not {@code null} | ||
* @return an object wrapping the string value | ||
*/ | ||
public static AnchorTargetValue forString(String value) { | ||
Optional<AnchorTarget> target = Stream.of(AnchorTarget.values()).filter( | ||
type -> type.getValue().equals(Objects.requireNonNull(value))) | ||
.findFirst(); | ||
if (target.isPresent()) { | ||
return target.get(); | ||
} | ||
return new AnchorTargetValue() { | ||
|
||
@Override | ||
public String getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (!obj.getClass().equals(getClass())) { | ||
return false; | ||
} | ||
return value.equals(((AnchorTargetValue) obj).getValue()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
}; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
flow-html-components/src/test/java/com/vaadin/flow/component/html/AnchorTargetValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2000-2021 Vaadin Ltd. | ||
* | ||
* 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 com.vaadin.flow.component.html; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class AnchorTargetValueTest { | ||
|
||
@Test | ||
public void fromString_notEnum_objectHasValueAndEquals() { | ||
AnchorTargetValue value = AnchorTargetValue.forString("foo"); | ||
Assert.assertEquals("foo", value.getValue()); | ||
|
||
AnchorTargetValue value1 = AnchorTargetValue.forString("foo"); | ||
Assert.assertEquals(value, value1); | ||
Assert.assertEquals(value.hashCode(), value1.hashCode()); | ||
} | ||
|
||
@Test | ||
public void fromString_enumValue_resultIsEnum() { | ||
AnchorTargetValue value = AnchorTargetValue | ||
.forString(AnchorTarget.TOP.getValue()); | ||
Assert.assertEquals(AnchorTarget.TOP, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters