All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.kernel.impl.util.Validators Maven / Gradle / Ivy

/*
 * Copyright (c) 2002-2015 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.kernel.impl.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.kernel.impl.store.record.NeoStoreUtil;

public class Validators
{
    public static final Validator FILE_EXISTS = new Validator()
    {
        @Override
        public void validate( File file )
        {
            if ( !file.exists() )
            {
                throw new IllegalArgumentException( "File '" + file + "' doesn't exist" );
            }
        }
    };

    public static final Validator REGEX_FILE_EXISTS = new Validator()
    {
        @Override
        public void validate( File file )
        {
            if ( matchingFiles( file ).isEmpty() )
            {
                throw new IllegalArgumentException( "File '" + file + "' doesn't exist" );
            }
        }
    };

    static List matchingFiles( File fileWithRegexInName )
    {
        File parent = fileWithRegexInName.getAbsoluteFile().getParentFile();
        if ( parent == null || !parent.exists() )
        {
            throw new IllegalArgumentException( "Directory of " + fileWithRegexInName + " doesn't exist" );
        }
        final Pattern pattern = Pattern.compile( fileWithRegexInName.getName() );
        List files = new ArrayList<>();
        for ( File file : parent.listFiles() )
        {
            if ( pattern.matcher( file.getName() ).matches() )
            {
                files.add( file );
            }
        }
        return files;
    }

    public static final Validator DIRECTORY_IS_WRITABLE = new Validator()
    {
        @Override
        public void validate( File value )
        {
            if ( value.mkdirs() )
            {   // It's OK, we created the directory right now, which means we have write access to it
                return;
            }

            File test = new File( value, "_______test___" );
            try
            {
                test.createNewFile();
            }
            catch ( IOException e )
            {
                throw new IllegalArgumentException( "Directoy '" + value + "' not writable: " + e.getMessage() );
            }
            finally
            {
                test.delete();
            }
        }
    };

    public static final Validator CONTAINS_NO_EXISTING_DATABASE = new Validator()
    {
        @Override
        public void validate( File value )
        {
            if ( NeoStoreUtil.neoStoreExists( new DefaultFileSystemAbstraction(), value ) )
            {
                throw new IllegalArgumentException( "Directory '" + value + "' already contains a database" );
            }
        }
    };

    public static  Validator atLeast( final String key, final int length )
    {
        return new Validator()
        {
            @Override
            public void validate( T[] value )
            {
                if ( value.length < length )
                {
                    throw new IllegalArgumentException( "Expected '" + key + "' to have at least " +
                            length + " item" + (length == 1 ? "" : "s") + ", but had " + value.length +
                            " (" + Arrays.toString( value ) + ")" );
                }
            }
        };
    }

    public static final  Validator emptyValidator()
    {
        return new Validator()
        {
            @Override
            public void validate( T value )
            {   // Do nothing
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy