001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.core.schema.types.constraints;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.Map;
025
026/**
027 * This constraint ensure some object is not null.
028 * <p>
029 * This class is a singleton. Use {@link #get()} to get the singleton.
030 * </p>
031 *
032 * @since 7.1
033 * @author <a href="mailto:nc@nuxeo.com">Nicolas Chapurlat</a>
034 */
035public class NotNullConstraint extends AbstractConstraint {
036
037    private static final long serialVersionUID = 1L;
038
039    private static final String NAME = "NotNullConstraint";
040
041    // Lazy ang thread safe singleton instance.
042    private static class Holder {
043        private static final NotNullConstraint INSTANCE = new NotNullConstraint();
044    }
045
046    private NotNullConstraint() {
047    }
048
049    public static NotNullConstraint get() {
050        return Holder.INSTANCE;
051    }
052
053    @Override
054    public boolean validate(Object object) {
055        return object != null;
056    }
057
058    /**
059     * Here, value is : <br>
060     * name = {@value #NAME}. <br>
061     * parameters is empty
062     */
063    @Override
064    public Description getDescription() {
065        Map<String, Serializable> params = new HashMap<>();
066        return new Description(NotNullConstraint.NAME, params);
067    }
068
069    @Override
070    public int hashCode() {
071        return NotNullConstraint.class.hashCode();
072    }
073
074    @Override
075    public boolean equals(Object obj) {
076        return obj != null && obj instanceof NotNullConstraint;
077    }
078}