Search in sources :

Example 61 with Contract

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;
}
Also used : PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) PolyStringTranslationType(com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType) Contract(org.jetbrains.annotations.Contract)

Example 62 with Contract

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;
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) Contract(org.jetbrains.annotations.Contract)

Example 63 with Contract

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectStreamClass(java.io.ObjectStreamClass) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) ObjectStreamClass(java.io.ObjectStreamClass) ObjectInputStream(java.io.ObjectInputStream) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 64 with Contract

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);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Example 65 with Contract

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;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Contract(org.jetbrains.annotations.Contract) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Contract (org.jetbrains.annotations.Contract)111 NotNull (org.jetbrains.annotations.NotNull)48 Nullable (org.jetbrains.annotations.Nullable)40 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)29 PsiElement (com.intellij.psi.PsiElement)19 Call (org.elixir_lang.psi.call.Call)17 ASTNode (com.intellij.lang.ASTNode)14 CallDefinitionClause.enclosingModularMacroCall (org.elixir_lang.structure_view.element.CallDefinitionClause.enclosingModularMacroCall)9 PsiFile (com.intellij.psi.PsiFile)6 IElementType (com.intellij.psi.tree.IElementType)6 BigInteger (java.math.BigInteger)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Quotable (org.elixir_lang.psi.Quotable)5 Project (com.intellij.openapi.project.Project)4 NavigatablePsiElement (com.intellij.psi.NavigatablePsiElement)3 Matcher (java.util.regex.Matcher)3 TreeElement (com.intellij.ide.util.treeView.smartTree.TreeElement)2 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)2 TextRange (com.intellij.openapi.util.TextRange)2 File (java.io.File)2