use of org.jetbrains.annotations.Contract in project midpoint by Evolveum.
the class WebComponentUtil method createPolyFromOrigString.
@Contract("null, _ -> null; !null, _ -> !null")
public static PolyStringType createPolyFromOrigString(String str, String key) {
if (str == null) {
return null;
}
PolyStringType poly = new PolyStringType();
poly.setOrig(str);
if (StringUtils.isNotEmpty(key)) {
PolyStringTranslationType translation = new PolyStringTranslationType();
translation.setKey(key);
poly.setTranslation(translation);
}
return poly;
}
use of org.jetbrains.annotations.Contract in project midpoint by Evolveum.
the class ObjectTreeDeltas method fromObjectTreeDeltasType.
@Contract("null -> null; !null -> !null")
public static ObjectTreeDeltas fromObjectTreeDeltasType(ObjectTreeDeltasType deltasType) throws SchemaException {
if (deltasType == null) {
return null;
}
PrismContext prismContext = PrismContext.get();
ObjectTreeDeltas deltas = new ObjectTreeDeltas(prismContext);
if (deltasType.getFocusPrimaryDelta() != null) {
// noinspection unchecked
deltas.setFocusChange(DeltaConvertor.createObjectDelta(deltasType.getFocusPrimaryDelta(), prismContext));
}
for (ProjectionObjectDeltaType projectionObjectDeltaType : deltasType.getProjectionPrimaryDelta()) {
// TODO reconsider providing default intent here
ResourceShadowDiscriminator rsd = ResourceShadowDiscriminator.fromResourceShadowDiscriminatorType(projectionObjectDeltaType.getResourceShadowDiscriminator(), true);
ObjectDelta objectDelta = DeltaConvertor.createObjectDelta(projectionObjectDeltaType.getPrimaryDelta(), prismContext);
// noinspection unchecked
deltas.addProjectionChange(rsd, objectDelta);
}
return deltas;
}
use of org.jetbrains.annotations.Contract in project Serial by twitter.
the class SerializableUtils method fromByteArray.
/**
* Deserializes an object from a byte array using regular Java serialization. The class mapping can be used
* to replace a class in the stream (identified by uid) by another class. The replacement class must have the
* same name and serialization id, but it may be located in a different package.
*/
@Nullable
@Contract("null, _ -> null")
public static <T> T fromByteArray(@Nullable byte[] bytes, @Nullable final Map<Long, Class<?>> classMapping) throws IOException, ClassNotFoundException {
if (bytes == null || bytes.length == 0) {
return null;
}
final ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
ObjectInputStream objectStream = null;
try {
if (classMapping == null) {
objectStream = new ObjectInputStream(byteStream);
} else {
objectStream = new ObjectInputStream(byteStream) {
@Override
@NotNull
protected Class<?> resolveClass(@NotNull ObjectStreamClass osClass) throws IOException, ClassNotFoundException {
final Class<?> resolvedClass = classMapping.get(osClass.getSerialVersionUID());
if (resolvedClass != null) {
return resolvedClass;
}
return super.resolveClass(osClass);
}
};
}
final T item = (T) objectStream.readObject();
return item;
} catch (IOException | ClassNotFoundException | IllegalStateException e) {
throw e;
} finally {
InternalSerialUtils.closeSilently(objectStream);
InternalSerialUtils.closeSilently(byteStream);
}
}
use of org.jetbrains.annotations.Contract in project Serial by twitter.
the class SerializableUtils method toByteArray.
/**
* Converts the specified object to a byte array, failing silently. If an exception occurs, null
* is returned.
*
* @param o The object to convert to a byte array.
* @return byte array or null if an exception occurs.
*/
@Nullable
@Contract("null -> null")
public static byte[] toByteArray(@Nullable Serializable o) throws IOException {
if (o == null) {
return null;
}
final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(BYTE_ARRAY_BUFFER_SIZE);
ObjectOutputStream objectStream = null;
try {
objectStream = new ObjectOutputStream(byteStream);
objectStream.writeObject(o);
return byteStream.toByteArray();
} finally {
InternalSerialUtils.closeSilently(objectStream);
InternalSerialUtils.closeSilently(byteStream);
}
}
use of org.jetbrains.annotations.Contract in project intellij-community by JetBrains.
the class RegExpLanguageHosts method findRegExpHost.
@Contract("null -> null")
@Nullable
private static RegExpLanguageHost findRegExpHost(@Nullable final PsiElement element) {
if (element == null) {
return null;
}
final PsiFile file = element.getContainingFile();
final PsiElement context = file.getContext();
if (context instanceof RegExpLanguageHost) {
return (RegExpLanguageHost) context;
}
if (context != null) {
return INSTANCE.forClass(context.getClass());
}
return null;
}
Aggregations