/*
 * Copyright (C) 2004-2007  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.instrument;

import java.lang.reflect.Modifier;


class Option
{
	final boolean exists;
	final int visibility;
	final String suffix;
	final boolean booleanAsIs;
	final boolean isFinal;
	
	Option(final String optionString, final boolean allowFinal)
	{
		if(optionString==null)
		{
			exists = true;
			visibility = Option.INHERITED;
			suffix = "";
			booleanAsIs = false;
			isFinal = allowFinal;
		}
		else
		{
			if(optionString.indexOf("none")>=0)
			{
				exists = false;
				visibility = -1;
				suffix = null;
			}
			else if(optionString.indexOf("internal")>=0)
			{
				exists = true;
				visibility = PRIVATE;
				suffix = "Internal";
			}
			else if(optionString.indexOf("private")>=0)
			{
				exists = true;
				visibility = PRIVATE;
				suffix = "";
			}
			else if(optionString.indexOf("protected")>=0)
			{
				exists = true;
				visibility = PROTECTED;
				suffix = "";
			}
			else if(optionString.indexOf("package")>=0)
			{
				exists = true;
				visibility = PACKAGE;
				suffix = "";
			}
			else if(optionString.indexOf("public")>=0)
			{
				exists = true;
				visibility = PUBLIC;
				suffix = "";
			}
			else
			{
				exists = true;
				visibility = INHERITED;
				suffix = "";
			}

			booleanAsIs = (optionString.indexOf("boolean-as-is")>=0);
			if(allowFinal)
				this.isFinal = (optionString.indexOf("non-final")<0);
			else
				this.isFinal = false;
		}
	}

	private static final int INHERITED = 0;
	private static final int PRIVATE = 1;
	private static final int PROTECTED = 2;
	private static final int PACKAGE = 3;
	private static final int PUBLIC = 4;
	
	final int getModifier(final int inheritedModifier)
	{
		if(!exists)
			throw new RuntimeException();
		
		final int result;
		switch(visibility)
		{
			case Option.INHERITED:
				result = inheritedModifier & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE);
				break;
			case Option.PRIVATE:
				result = Modifier.PRIVATE;
				break;
			case Option.PROTECTED:
				result = Modifier.PROTECTED;
				break;
			case Option.PACKAGE:
				result = 0;
				break;
			case Option.PUBLIC:
				result = Modifier.PUBLIC;
				break;
			default:
				throw new RuntimeException(String.valueOf(visibility));
		}
		if(isFinal)
			return result | Modifier.FINAL;
		else
			return result;
	}
	
}

@@ -870,17 +870,16 @@
 ifier;%0A%0A
-%0A
 class Op
@@ -885,16 +885,516 @@
 ption%0A%7B%0A
+%09private static final String TEXT_NONE = %22none%22;%0A%09private static final String TEXT_INTERNAL = %22internal%22;%0A%09private static final String TEXT_VISIBILITY_PRIVATE = %22private%22;%0A%09private static final String TEXT_VISIBILITY_PROTECTED = %22protected%22;%0A%09private static final String TEXT_VISIBILITY_PACKAGE = %22package%22;%0A%09private static final String TEXT_VISIBILITY_PUBLIC = %22public%22;%0A%09private static final String TEXT_BOOLEAN_AS_IS = %22boolean-as-is%22;%0A%09private static final String TEXT_NON_FINAL = %22non-final%22;%0A%09%0A
 %09final b
@@ -1759,22 +1759,25 @@
 indexOf(
-%22none%22
+TEXT_NONE
 )%3E=0)%0A%09%09
@@ -1876,26 +1876,29 @@
 indexOf(
-%22internal%22
+TEXT_INTERNAL
 )%3E=0)%0A%09%09
@@ -2007,25 +2007,39 @@
 indexOf(
-%22private%22
+TEXT_VISIBILITY_PRIVATE
 )%3E=0)%0A%09%09
@@ -2140,27 +2140,41 @@
 indexOf(
-%22protected%22
+TEXT_VISIBILITY_PROTECTED
 )%3E=0)%0A%09%09
@@ -2277,25 +2277,39 @@
 indexOf(
-%22package%22
+TEXT_VISIBILITY_PACKAGE
 )%3E=0)%0A%09%09
@@ -2410,24 +2410,38 @@
 indexOf(
-%22public%22
+TEXT_VISIBILITY_PUBLIC
 )%3E=0)%0A%09%09
@@ -2631,31 +2631,34 @@
 indexOf(
-%22boolean-as-is%22
+TEXT_BOOLEAN_AS_IS
 )%3E=0);%0A%09
@@ -2715,27 +2715,30 @@
 indexOf(
-%22non-final%22
+TEXT_NON_FINAL
 )%3C0);%0A%09%09
@@ -3660,12 +3660,10 @@
 ult;%0A%09%7D%0A
-%09%0A
 %7D%0A

use string constants