@@ -0,0 +1,3255 @@
+/*-%0A * Copyright (c) 2013 Red Hat, Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage org.fedoraproject.maven.utils;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.io.RandomAccessFile;%0Aimport java.nio.channels.FileLock;%0A%0A/**%0A * Atomic integer value, which stores its value in a text file.%0A * %0A * @author Mikolaj Izdebski%0A */%0Apublic class AtomicFileCounter%0A%7B%0A    private static final int BUFSIZ = 64;%0A%0A    private final byte%5B%5D buffer = new byte%5BBUFSIZ%5D;%0A%0A    private final RandomAccessFile file;%0A%0A    /**%0A     * Create an instance of counter.%0A     * %0A     * @param path path to the backing file%0A     * @throws IOException%0A     */%0A    public AtomicFileCounter( String path )%0A        throws IOException%0A    %7B%0A        file = new RandomAccessFile( new File( path ), %22rw%22 );%0A    %7D%0A%0A    /**%0A     * Create an instance of counter and set its initial value.%0A     * %0A     * @param path path to the backing file%0A     * @param value initial value of the counter%0A     * @throws IOException%0A     */%0A    public AtomicFileCounter( String path, int value )%0A        throws IOException%0A    %7B%0A        this( path );%0A        setValue( value );%0A    %7D%0A%0A    private FileLock lock()%0A        throws IOException%0A    %7B%0A        return file.getChannel().lock();%0A    %7D%0A%0A    private int readValue()%0A        throws IOException%0A    %7B%0A        file.seek( 0 );%0A        long length = file.length();%0A        if ( length %3E BUFSIZ )%0A            throw new IOException( %22Semaphore file is too large%22 );%0A        file.readFully( buffer, 0, (int) length );%0A        String text = new String( buffer, 0, (int) length, %22US-ASCII%22 );%0A        return Integer.parseInt( text );%0A    %7D%0A%0A    private void writeValue( int value )%0A        throws IOException%0A    %7B%0A        file.seek( 0 );%0A        file.setLength( 0 );%0A        file.write( String.format( %22%25d%25n%22, value ).getBytes( %22US-ASCII%22 ) );%0A    %7D%0A%0A    /**%0A     * If counter is positive then decrement it.%0A     * %0A     * @return the initial value of the counter (before decrementing)%0A     * @throws IOException%0A     */%0A    public int tryDecrement()%0A        throws IOException%0A    %7B%0A        try (FileLock lock = lock())%0A        %7B%0A            int value = readValue();%0A            if ( value %3E 0 )%0A                writeValue( value - 1 );%0A            return value;%0A        %7D%0A    %7D%0A%0A    /**%0A     * Get value of the counter.%0A     * %0A     * @return value of the counter%0A     * @throws IOException%0A     */%0A    public int getValue()%0A        throws IOException%0A    %7B%0A        try (FileLock lock = lock())%0A        %7B%0A            return readValue();%0A        %7D%0A    %7D%0A%0A    private void setValue( int value )%0A        throws IOException%0A    %7B%0A        try (FileLock lock = lock())%0A        %7B%0A            writeValue( value );%0A        %7D%0A    %7D%0A%7D%0A

Implement AtomicFileCounter