/*
 * Copyright (c) 2015 Cisco Systems, Inc. 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
 */
package org.opendaylight.yangtools.yang.parser.spi.source;

import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;

/**
 *
 * Reference of statement source present in textual source format
 *
 * Utility implementation of {@link StatementSourceReference} for textual sources,
 * this is prefered {@link StatementSourceReference} for implementations
 * of YANG / YIN statement stream sources.
 *
 *
 *  To create source reference use one of this static factories:
 *  <ul>
 *  <li>{@link #atPosition(String, int, int)} - provides most specific reference of statement location,
 *  this is most prefered since it provides most context to debug YANG model.
 *  </li>
 *  <li>{@link #atLine(String, int)}- provides source and line of statement location.
 *  </li>
 *  <li>{@link #inSource(String)} - least specific reference, should be used only if any of previous
 *  references are unable to create / derive from source.
 *  </li>
 *  </ul>
 *
 */
public abstract class DeclarationInTextSource implements StatementSourceReference {

    private final String sourceName;

    DeclarationInTextSource(String sourceName) {
        this.sourceName = sourceName;
    }

    public String getSourceName() {
        return sourceName;
    }

    @Override
    public StatementSource getStatementSource() {
        return StatementSource.DECLARATION;
    }

    @Override
    public abstract String toString();

    public static DeclarationInTextSource inSource(String sourceName) {
        return new InSource(sourceName);
    }

    public static DeclarationInTextSource atLine(String sourceName, int line) {
        return new AtLine(sourceName, line);
    }

    public static DeclarationInTextSource atPosition(String sourceName, int line, int position) {
        return new AtPosition(sourceName, line,position);
    }

    private static class InSource extends DeclarationInTextSource {

        InSource(String sourceName) {
            super(sourceName);
        }

        @Override
        public String toString() {
            return getSourceName();
        }

    }

    private static class AtLine extends InSource {

        private final int line;

        AtLine(String sourceName, int line) {
            super(sourceName);
            this.line = line;
        }

        @Override
        public String toString() {
            return String.format("%s:%d", getSourceName(),line);
        }

        public int getLine() {
            return line;
        }

    }

    private static class AtPosition extends AtLine {

        private final int character;

        AtPosition(String sourceName, int line, int character) {
            super(sourceName, line);
            this.character = character;
        }

        @Override
        public String toString() {
            return String.format("%s:%d:%d", getSourceName(),getLine(),character);
        }

    }

}

@@ -1409,24 +1409,30 @@
 nTextSource(
+final 
 String sourc
@@ -1767,32 +1767,38 @@
 Source inSource(
+final 
 String sourceNam
@@ -1895,24 +1895,30 @@
 urce atLine(
+final 
 String sourc
@@ -1915,32 +1915,38 @@
 ring sourceName,
+ final
  int line) %7B%0A   
@@ -2039,32 +2039,38 @@
 urce atPosition(
+final 
 String sourceNam
@@ -2063,32 +2063,38 @@
 ring sourceName,
+ final
  int line, int p
@@ -2083,24 +2083,30 @@
 al int line,
+ final
  int positio
@@ -2235,33 +2235,32 @@
 nInTextSource %7B%0A
-%0A
         InSource
@@ -2256,24 +2256,30 @@
    InSource(
+final 
 String sourc
@@ -2420,33 +2420,32 @@
 me();%0A        %7D%0A
-%0A
     %7D%0A%0A    priva
@@ -2531,24 +2531,30 @@
      AtLine(
+final 
 String sourc
@@ -2551,32 +2551,38 @@
 ring sourceName,
+ final
  int line) %7B%0A   
@@ -2725,31 +2725,8 @@
 turn
- String.format(%22%25s:%25d%22,
  get
@@ -2741,14 +2741,21 @@
 me()
-,
+ + ':' + 
 line
-)
 ;%0A  
@@ -2825,25 +2825,24 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%0A    p
@@ -2943,24 +2943,30 @@
  AtPosition(
+final 
 String sourc
@@ -2971,16 +2971,22 @@
 rceName,
+ final
  int lin
@@ -2987,16 +2987,22 @@
 nt line,
+ final
  int cha
@@ -3175,60 +3175,50 @@
 urn 
-String.format(%22%25s:%25d:%25d%22, getSourceName(),getLine(),
+getSourceName() + ':' + getLine() + ':' + 
 char
@@ -3222,17 +3222,16 @@
 haracter
-)
 ;%0A      
@@ -3234,18 +3234,16 @@
       %7D%0A
-%0A
     %7D%0A
-%0A
 %7D%0A

Optimize toString() methods