view dwtx/jface/text/CopyOnWriteTextStore.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2005, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Anton Leherbauer (anton.leherbauer@windriver.com) - initial API and implementation
 * Port to the D programming language:
 *     Frank Benoit <benoit@tionex.de>
 *******************************************************************************/
module dwtx.jface.text.CopyOnWriteTextStore;

import dwt.dwthelper.utils;

import dwtx.core.runtime.Assert;


/**
 * Copy-on-write <code>ITextStore</code> wrapper.
 * <p>
 * This implementation uses an unmodifiable text store for the initial content.
 * Upon first modification attempt, the unmodifiable store is replaced with
 * a modifiable instance which must be supplied in the constructor.</p>
 * <p>
 * This class is not intended to be subclassed.
 * </p>
 *
 * @since 3.2
 * @noextend This class is not intended to be subclassed by clients.
 */
public class CopyOnWriteTextStore : ITextStore {

    /**
     * An unmodifiable String based text store. It is not possible to modify the content
     * other than using {@link #set}. Trying to {@link #replace} a text range will
     * throw an <code>UnsupportedOperationException</code>.
     */
    private static class StringTextStore : ITextStore {

        /** Represents the content of this text store. */
        private String fText= ""; //$NON-NLS-1$

        /**
         * Create an empty text store.
         */
        private StringTextStore() {
            super();
        }

        /**
         * Create a text store with initial content.
         * @param text  the initial content
         */
        private StringTextStore(String text) {
            super();
            set(text);
        }

        /*
         * @see dwtx.jface.text.ITextStore#get(int)
         */
        public char get(int offset) {
            return fText.charAt(offset);
        }

        /*
         * @see dwtx.jface.text.ITextStore#get(int, int)
         */
        public String get(int offset, int length) {
            return fText.substring(offset, offset + length);
        }

        /*
         * @see dwtx.jface.text.ITextStore#getLength()
         */
        public int getLength() {
            return fText.length();
        }

        /*
         * @see dwtx.jface.text.ITextStore#replace(int, int, java.lang.String)
         */
        public void replace(int offset, int length, String text) {
            // modification not supported
            throw new UnsupportedOperationException();
        }

        /*
         * @see dwtx.jface.text.ITextStore#set(java.lang.String)
         */
        public void set(String text) {
            fText= text !is null ? text : ""; //$NON-NLS-1$
        }

    }

    /** The underlying "real" text store */
    protected ITextStore fTextStore= new StringTextStore();

    /** A modifiable <code>ITextStore</code> instance */
    private final ITextStore fModifiableTextStore;

    /**
     * Creates an empty text store. The given text store will be used upon first
     * modification attempt.
     * 
     * @param modifiableTextStore
     *            a modifiable <code>ITextStore</code> instance, may not be
     *            <code>null</code>
     */
    public CopyOnWriteTextStore(ITextStore modifiableTextStore) {
        Assert.isNotNull(modifiableTextStore);
        fTextStore= new StringTextStore();
        fModifiableTextStore= modifiableTextStore;
    }

    /*
     * @see dwtx.jface.text.ITextStore#get(int)
     */
    public char get(int offset) {
        return fTextStore.get(offset);
    }

    /*
     * @see dwtx.jface.text.ITextStore#get(int, int)
     */
    public String get(int offset, int length) {
        return fTextStore.get(offset, length);
    }

    /*
     * @see dwtx.jface.text.ITextStore#getLength()
     */
    public int getLength() {
        return fTextStore.getLength();
    }

    /*
     * @see dwtx.jface.text.ITextStore#replace(int, int, java.lang.String)
     */
    public void replace(int offset, int length, String text) {
        if (fTextStore !is fModifiableTextStore) {
            String content= fTextStore.get(0, fTextStore.getLength());
            fTextStore= fModifiableTextStore;
            fTextStore.set(content);
        }
        fTextStore.replace(offset, length, text);
    }

    /*
     * @see dwtx.jface.text.ITextStore#set(java.lang.String)
     */
    public void set(String text) {
        fTextStore= new StringTextStore(text);
        fModifiableTextStore.set(""); //$NON-NLS-1$
    }

}