-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ClientCore] Introduce Union type (#43778)
- Loading branch information
Showing
11 changed files
with
1,285 additions
and
0 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
88 changes: 88 additions & 0 deletions
88
...ntcore/core/src/main/java/io/clientcore/core/implementation/GenericParameterizedType.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,88 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package io.clientcore.core.implementation; | ||
|
||
import io.clientcore.core.instrumentation.logging.ClientLogger; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A {@link ParameterizedType} implementation that allows for reference type arguments. | ||
*/ | ||
public final class GenericParameterizedType implements ParameterizedType { | ||
private static final ClientLogger LOGGER = new ClientLogger(GenericParameterizedType.class); | ||
|
||
private final Class<?> raw; | ||
private final Type[] args; | ||
private String cachedToString; | ||
|
||
/** | ||
* Creates a new instance of {@link GenericParameterizedType}. | ||
* | ||
* @param raw The raw type. | ||
* @param args The type arguments. | ||
*/ | ||
public GenericParameterizedType(Class<?> raw, Type... args) { | ||
this.raw = raw; | ||
|
||
if (args == null) { | ||
throw LOGGER.logThrowableAsError(new IllegalArgumentException("args cannot be null")); | ||
} | ||
|
||
Type[] argsCopy = new Type[args.length]; | ||
for (int i = 0; i < args.length; i++) { | ||
if (args[i] == null) { | ||
throw LOGGER.logThrowableAsError( | ||
new IllegalArgumentException("args cannot contain null: null value in index " + i)); | ||
} | ||
argsCopy[i] = args[i]; | ||
} | ||
this.args = argsCopy; | ||
} | ||
|
||
@Override | ||
public Type[] getActualTypeArguments() { | ||
return args; | ||
} | ||
|
||
@Override | ||
public Type getRawType() { | ||
return raw; | ||
} | ||
|
||
@Override | ||
public Type getOwnerType() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (cachedToString == null) { | ||
cachedToString = raw.getTypeName() + "<" | ||
+ Arrays.stream(args).map(Type::getTypeName).collect(Collectors.joining(", ")) + ">"; | ||
} | ||
return cachedToString; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
GenericParameterizedType that = (GenericParameterizedType) o; | ||
return Objects.equals(raw, that.raw) && Objects.deepEquals(args, that.args); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(raw, Arrays.hashCode(args)); | ||
} | ||
} |
Oops, something went wrong.