john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

PropertiesDuplicateChecker

// 2012-07-05 johnpfeiffer only checks for duplicates in lines containing =

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class PropertiesDuplicateChecker
{
    private static final String CLASSVERSION = "0.2";
    private ArrayList <String> keys;

    PropertiesDuplicateChecker( String pathFile ) throws IOException , IllegalArgumentException
    {
        if( pathFile == null )
        {
            throw new IllegalArgumentException( "ERROR: Path and File cannot be null" );
        }
        if( pathFile.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: Path and File cannot be empty" );
        }
        initialize( pathFile );
    }

    private void initialize( String pathFile ) throws IOException , IllegalArgumentException
    {
        if( pathFile == null )
        {
            throw new IllegalArgumentException( "ERROR: Path and File cannot be null" );
        }
        if( pathFile.isEmpty() )
        {
            throw new IllegalArgumentException( "ERROR: Path and File cannot be empty" );
        }

        keys = new ArrayList <String>();
        FileInputStream fileInputStream = new FileInputStream( pathFile );
        InputStreamReader inStream = new InputStreamReader( fileInputStream );
        BufferedReader br = new BufferedReader( inStream );
        String line;
        String lineArray[] = null;
        while( ( line = br.readLine() ) != null )
        {
            if( line.contains( "=" ) )
            {
                lineArray = line.split( "=" );
                keys.add( lineArray[0] );
            }
        }
        br.close();
    }

    protected String getVersion()
    {
        return CLASSVERSION;
    }

    private boolean containsDuplicateKeysAlternate()
    {
        boolean result = false;
        Set <String> set = new HashSet <String>( keys );
        if( set.size() < keys.size() )
        {
            result = true;
        }
        return result;
    }

    protected boolean containsDuplicateKeys()
    {
        boolean result = false;
        ArrayList <String> duplicates = getDuplicateKeys();
        if( duplicates.size() > 0 )
        {
            result = true;
        }
        return result;
    }


    protected ArrayList <String> getDuplicateKeys()
    {
        ArrayList <String> duplicates = new ArrayList<String> ();
        Collections.sort( keys );
        if( keys.size() > 1 )
        {
            String previous;
            String current;
            for( int i = 1 ; i < keys.size() ; i++ )
            {
                previous = keys.get( i - 1 );
                current = keys.get( i );
                if( current.equals( previous ) && !current.isEmpty() )
                {
                    duplicates.add( current );
                }
            }
        }
        return duplicates;
    }

    static public void main( String args[] ) throws Exception
    {
        if( args != null && args.length != 1 )
        {
            System.out.println( "Incorrect number of parameters, java -jar PropertiesDuplicateChecker pathAndFilename.properties" );
        }
        String pathFile = args[0];
        PropertiesDuplicateChecker main = new PropertiesDuplicateChecker( pathFile );
        System.out.println( "Contains Duplicate? " + main.containsDuplicateKeys() );
        System.out.println( "Contains Duplicate (alternate test)? " + main.containsDuplicateKeysAlternate() );
        System.out.println( "Duplicate Keys: " + System.getProperty( "line.separator" ) + main.getDuplicateKeys() );
    }

} // end class

  • « PropertiesDuplicateCheckerTest
  • Tomcat6 ssl generate openssl cert »

Published

Jul 6, 2012

Category

java-classes

~273 words

Tags

  • classes 92
  • java 252
  • propertiesduplicatechecker 1