io.twipple.springframework.data.clusterpoint.convert.ClusterpointDocumentPropertyAccessor.java Source code

Java tutorial

Introduction

Here is the source code for io.twipple.springframework.data.clusterpoint.convert.ClusterpointDocumentPropertyAccessor.java

Source

/*
 * This file is part of Spring Data Clusterpoint.
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 the author or authors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package io.twipple.springframework.data.clusterpoint.convert;

import io.twipple.springframework.data.clusterpoint.mapping.ClusterpointDocument;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import org.springframework.util.Assert;

import javax.validation.constraints.NotNull;

/**
 * A property accessor for document properties.
 *
 * @author Olegs Briska
 */
public class ClusterpointDocumentPropertyAccessor implements PropertyAccessor {

    /**
     * Contains the static instance of this accessor.
     */
    public static final ClusterpointDocumentPropertyAccessor INSTANCE = new ClusterpointDocumentPropertyAccessor();
    /**
     * Target classes of the properties.
     */
    private static final Class<?>[] TARGET_CLASSES = new Class[] { ClusterpointDocument.class };

    /**
     * Returns the target classes of the properties.
     *
     * @return
     */
    @Override
    @NotNull
    public Class<?>[] getSpecificTargetClasses() {
        return TARGET_CLASSES;
    }

    /**
     * It can always read from those properties.
     *
     * @param context the evaluation context.
     * @param target  the target object.
     * @param name    the name of the property.
     * @return always true.
     */
    @Override
    public boolean canRead(@NotNull EvaluationContext context, @NotNull Object target, @NotNull String name) {

        Assert.notNull(context);
        Assert.notNull(target);
        Assert.isInstanceOf(ClusterpointDocument.class, target);
        Assert.hasText(name);

        ClusterpointDocument source = (ClusterpointDocument) target;

        return source.canRead(context, name);
    }

    /**
     * Read the value from the property.
     *
     * @param context the evaluation context.
     * @param target  the target object.
     * @param name    the name of the property.
     * @return the typed value of the content to be read.
     */
    @Override
    public TypedValue read(@NotNull EvaluationContext context, @NotNull Object target, @NotNull String name) {

        Assert.notNull(context);
        Assert.notNull(target);
        Assert.isInstanceOf(ClusterpointDocument.class, target);
        Assert.hasText(name);

        ClusterpointDocument source = (ClusterpointDocument) target;

        Object value = source.read(context, name);
        return value == null ? TypedValue.NULL : new TypedValue(value);
    }

    @Override
    public boolean canWrite(@NotNull EvaluationContext context, @NotNull Object target, @NotNull String name)
            throws AccessException {

        Assert.notNull(context);
        Assert.notNull(target);
        Assert.isInstanceOf(ClusterpointDocument.class, target);
        Assert.hasText(name);

        ClusterpointDocument source = (ClusterpointDocument) target;

        return source.canWrite(context, name);
    }

    @Override
    public void write(@NotNull EvaluationContext context, @NotNull Object target, @NotNull String name,
            Object newValue) throws AccessException {

        Assert.notNull(context);
        Assert.notNull(target);
        Assert.isInstanceOf(ClusterpointDocument.class, target);
        Assert.hasText(name);

        ClusterpointDocument source = (ClusterpointDocument) target;

        source.write(context, name, newValue);
    }
}