package nl.tudelft.graphalytics.util.graph;

import nl.tudelft.graphalytics.util.graph.PropertyGraphParser.ValueParser;

import java.io.IOException;
import java.util.Arrays;

/**
 * Collections of commonly used PropertyGraph.ValueParser implementations.
 *
 * @author Tim Hegeman
 */
public class PropertyGraphValueParsers {

	/**
	 * Creates a parser that returns the value null with type Void, regardless of the input.
	 *
	 * @return a ValueParser for type Void
	 */
	public static ValueParser<Void> voidParser() {
		return new ValueParser<Void>() {
			@Override
			public Void parse(String[] valueTokens) throws IOException {
				return null;
			}
		};
	}

	/**
	 * Creates a parser that expects exactly one property and interprets it as a Long value.
	 * 
	 * @return a ValueParser for type Long
	 */
	public static ValueParser<Long> longParser() {
		return new ValueParser<Long>() {
			@Override
			public Long parse(String[] valueTokens) throws IOException {
				if (valueTokens.length != 1) {
					throw new IOException("Expected single property with value of type Long.");
				}

				try {
					return Long.parseLong(valueTokens[0]);
				} catch (NumberFormatException ex) {
					throw new IOException("Expected single property with value of type Long.", ex);
				}
			}
		};
	}

	/**
	 * Creates a parser that expects exactly one property and interprets it as a Double value.
	 *
	 * @return a ValueParser for type Double
	 */
	public static ValueParser<Double> doubleParser() {
		return new ValueParser<Double>() {
			@Override
			public Double parse(String[] valueTokens) throws IOException {
				if (valueTokens.length != 1) {
					throw new IOException("Expected single property with value of type Double.");
				}

				try {
					return Double.parseDouble(valueTokens[0]);
				} catch (NumberFormatException ex) {
					throw new IOException("Expected single property with value of type Double.", ex);
				}
			}
		};
	}

	/**
	 * Creates a parser that returns a copy of the input properties without any interpretation of the values.
	 *
	 * @return a ValueParser that returns a copy of the input
	 */
	public static ValueParser<String[]> identityParser() {
		return new ValueParser<String[]>() {
			@Override
			public String[] parse(String[] valueTokens) throws IOException {
				return Arrays.copyOf(valueTokens, valueTokens.length);
			}
		};
	}

}

@@ -1,8 +1,626 @@
+/*%0A * Copyright 2015 Delft University of Technology%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *         http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 

Add license to PropertyGraphValueParsers