/*
 * Copyright (C) 2020 Narongrit Unwerawattana <narongrit.kie@gmail.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., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

package jolie.lang.parse.module;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;

import jolie.lang.CodeCheckingError;
import jolie.lang.Constants;
import jolie.lang.parse.context.ParsingContext;

public class ModuleException extends Exception {
	private static final long serialVersionUID = Constants.serialVersionUID();
	private ParsingContext context = null;

	public ModuleException( ParsingContext context, String message ) {
		super( message );
		this.context = context;
	}

	public ModuleException( String message ) {
		super( message );
	}

	public ModuleException( ParsingContext context, Throwable arg1 ) {
		super( arg1 );
		this.context = context;
	}

	public ModuleException( Collection< CodeCheckingError > errors ) {
		super(
			errors.stream().map( CodeCheckingError::toString ).collect( Collectors.joining( "\n" ) ) );
	}

	public void setContext( ParsingContext context ) {
		this.context = context;
	}

	public Optional< ParsingContext > context() {
		return Optional.of( context );
	}

	@Override
	public String getMessage() {
		if( context != null ) {
			return new StringBuilder().append( context.sourceName() ).append( ':' )
				.append( context.line() ).append( ": error: " ).append( super.getMessage() )
				.toString();
		}
		return super.getMessage();
	}
}

@@ -1195,16 +1195,22 @@
 private 
+final 
 ParsingC
@@ -1227,15 +1227,8 @@
 text
- = null
 ;%0A%0A%09
@@ -1404,24 +1404,47 @@
  message );%0A
+%09%09this.context = null;%0A
 %09%7D%0A%0A%09public 
@@ -1726,64 +1726,8 @@
  );%0A
-%09%7D%0A%0A%09public void setContext( ParsingContext context ) %7B%0A
 %09%09th
@@ -1731,39 +1731,36 @@
 %09this.context = 
-context
+null
 ;%0A%09%7D%0A%0A%09public Op
@@ -1796,16 +1796,16 @@
 ext() %7B%0A
-
 %09%09return
@@ -1816,16 +1816,24 @@
 ional.of
+Nullable
 ( contex

Simplify ModuleException and fix use of Optional therein