package org.drools.compiler.lang.descr;

import java.util.HashMap;
import java.util.Map;

/*
 * Copyright 2011 JBoss Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class AnnotationDescr extends BaseDescr {
    
    private static final String VALUE            = "value";
    private static final long   serialVersionUID = 520l;

    private final String        name;
    private Map<String, String> values;

    // '' and 'a' are passed through as 
    public static String unquote( String s ){
        if( s.startsWith( "\"" ) && s.endsWith( "\"" ) ||
            s.startsWith( "'" ) && s.endsWith( "'" ) ) {
            return s.substring( 1, s.length() - 1 );
        } else {
            return s;
        }
    }
    
    public AnnotationDescr(final String name) {
        this.name = name;
        this.values = new HashMap<String, String>();
    }

    public AnnotationDescr(final String name,
                           final String value) {
        this.name = name;
        this.values = new HashMap<String, String>();
        this.values.put( VALUE, value );
    }

    public String getName() {
        return this.name;
    }
    
    public boolean hasValue() {
        return !this.values.isEmpty();
    }

    public void setValue( final String value ) {
        this.values.put( VALUE, value );
    }

    public void setKeyValue( final String key,
                             final String value ) {
        this.values.put( key, value );
    }

    public String getValue( final String key ) {
        return this.values.get( key );
    }
    
    public Map<String,String> getValues(){
        return this.values;
    }
    
    /**
     * Returns the metadata value as a single object or a Map
     * @return
     */
    public Object getValue() {
        Object single = getSingleValue();
        return single != null ? single : this.values;
    }

    public Object getValueStripped() {
        Object single = getSingleValueStripped();
        if( single != null ) return single;
        Map<String,String> sMap = new HashMap<String,String>();
        for( Map.Entry<String,String> entry: this.values.entrySet() ){
            sMap.put( entry.getKey(), unquote( entry.getValue() ) );
        }        
        return sMap;
    }

    public Map<String,String> getValueMap() {
        return this.values;
    }

    public String getSingleValue(){
        if( values.size() == 1 && values.containsKey( "value" ) ){
            return this.values.get( "value" );
        } else {
            return null;
        }
    }
    
    public String getSingleValueStripped(){
        if( values.size() == 1 && values.containsKey( "value" ) ){
            return unquote( this.values.get( "value" ) );
        } else {
            return null;
        }
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals( Object obj ) {
        if ( this == obj ) return true;
        if ( obj == null ) return false;
        if ( getClass() != obj.getClass() ) return false;
        AnnotationDescr other = (AnnotationDescr) obj;
        if ( name == null ) {
            if ( other.name != null ) return false;
        } else if ( !name.equals( other.name ) ) return false;
        return true;
    }

    public String getValuesAsString() {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for ( String key : values.keySet() ) {
            if ( ! first ) {
                sb.append( "," );
            }
            sb.append( key ).append( "=" ).append( values.get( key ) );
            if ( first ) {
                first = false;
            }
        }
        return sb.toString();
    }
}

@@ -3965,24 +3965,167 @@
 sString() %7B%0A
+        switch ( values.size() )  %7B%0A            case 0 : return %22%22;%0A            case 1 : return getSingleValue();%0A            default:%0A        
         Stri
@@ -4160,24 +4160,27 @@
 lder();%0A
+%0A  
         
 boolean 
@@ -4171,16 +4171,22 @@
         
+      
 boolean 
@@ -4207,16 +4207,24 @@
         
+ 
+       
 for ( St
@@ -4250,24 +4250,32 @@
 eySet() ) %7B%0A
+        
             
@@ -4299,32 +4299,40 @@
                 
+        
 sb.append( %22,%22 )
@@ -4337,34 +4337,50 @@
  );%0A            
+       
+ 
 %7D%0A
+        
             sb.a
@@ -4443,24 +4443,32 @@
             
+        
 if ( first )
@@ -4486,16 +4486,24 @@
         
+   
+     
 first = 
@@ -4525,28 +4525,48 @@
     
+     
+   %7D%0A                
 %7D%0A    
-    %7D%0A
         
 retu
@@ -4553,32 +4553,36 @@
   %7D%0A            
+    
 return sb.toStri
@@ -4586,16 +4586,27 @@
 tring();
+%0A%0A        %7D
 %0A    %7D%0A%7D

Support internal @Annotations with default values