Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2015-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* 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.
*
* Attribution Notice under the terms of the Apache License 2.0
*
* This work was created by the collective efforts of the openCypher community.
* Without limiting the terms of Section 6, any Derivative Work that is not
* approved by the public consensus process of the openCypher Implementers Group
* should not be described as “Cypher” (and Cypher® is a registered trademark of
* Neo4j Inc.) or as "openCypher". Extensions by implementers or prototypes or
* proposals for change that have been documented or implemented should only be
* described as "implementation extensions to Cypher" or as "proposed changes to
* Cypher that are not yet approved by the openCypher community".
*/
package org.opencypher.tools.io;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.CharBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Formatter;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.IntFunction;
import static java.lang.Character.charCount;
import static java.lang.Character.highSurrogate;
import static java.lang.Character.isBmpCodePoint;
import static java.lang.Character.isValidCodePoint;
import static java.lang.Character.lowSurrogate;
/**
* Unifies the different classes that can handle text output and formatting, such as {@link PrintStream}, {@link
* PrintWriter}, and {@link StringBuilder}.
*
* The methods in this interface are defined to return the {@code Output} instance itself ({@code this}), in order to
* allow chaining method calls in a "fluent" style.
*/
public interface Output extends Appendable, Closeable
{
/**
* Adapt an {@link OutputStream} to the Output interface.
*
* @param stream the {@link OutputStream} to adapt.
* @return an {@code Output} instance that writes to the supplied {@link OutputStream}.
*/
static Output output( OutputStream stream )
{
return new StreamOutput( stream instanceof PrintStream ? (PrintStream) stream : new PrintStream( stream ) );
}
/**
* Adapt a {@link Writer} to the Output interface.
*
* @param writer the {@link Writer} to adapt.
* @return an {@code Output} instance that writes to the supplied {@link Writer}.
*/
static Output output( Writer writer )
{
if ( writer instanceof OutputWriter )
{
return ((OutputWriter) writer).output;
}
return new WriterOutput( writer instanceof PrintWriter ? (PrintWriter) writer : new PrintWriter( writer ) );
}
/**
* Adapt a {@link StringBuilder} to the Output interface.
*
* @param builder the {@link StringBuilder} to adapt.
* @return a {@linkplain Readable readable} {@code Output} instance that writes to the supplied {@link
* StringBuilder}.
*/
static Readable output( StringBuilder builder )
{
return new StringBuilderOutput( builder );
}
/**
* Adapt a {@link StringBuffer} to the Output interface.
*
* @param buffer the {@link StringBuffer} to adapt.
* @return a {@linkplain Readable readable} {@code Output} instance that writes to the supplied {@link
* StringBuffer}.
*/
static Readable output( StringBuffer buffer )
{
return new StringBufferOutput( buffer );
}
/**
* Adapt a {@link CharBuffer} to the Output interface.
*
* @param buffer the {@link CharBuffer} to adapt.
* @return an {@code Output} instance that writes to the supplied {@link CharBuffer}.
*/
static Output output( CharBuffer buffer )
{
return new BufferOutput( buffer );
}
/**
* Create an {@code Output} that writes to the specified file.
*
* @param path the file to write to.
* @return an {@code Output} instance that writes to the specified file.
*/
static Output output( Path path )
{
try
{
return output( Files.newOutputStream( path ) );
}
catch ( IOException e )
{
throw new IllegalStateException( "Failed to open " + path, e );
}
}
/**
* Create a new {@code Output} that writes to a new string builder. The returned object can be used to {@linkplain
* Readable#toString() retrieve the constructed string}.
*
* @return a new {@linkplain Readable readable} {@code Output} instance.
*/
static Readable stringBuilder()
{
return output( new StringBuilder() );
}
/**
* Create a new {@code Output} that writes to a new string builder of given capacity. The returned object can be
* used to {@linkplain Readable#toString() retrieve the constructed string}.
*
* @param size the {@linkplain StringBuilder#StringBuilder(int) initial capacity} of the internal string builder.
* @return a new {@linkplain Readable readable} {@code Output} instance.
*/
static Output stringBuilder( int size )
{
return output( new StringBuilder( size ) );
}
/**
* Get an {@code Output} instance that writes to {@link System#out the standard output stream}.
*
* @return an {@code Output} instance that writes to {@link System#out standard out}.
*/
static Output stdOut()
{
return output( System.out );
}
/**
* Get an {@code Output} instance that writes to {@link System#err the standard error output stream}.
*
* @return an {@code Output} instance that writes to {@link System#err standard error}.
*/
static Output stdErr()
{
return output( System.err );
}
/**
* Get an {@code Output} instance that writes nowhere.
*
* The returned instance is {@linkplain Readable readable} in order to be usable in places where a {@linkplain
* Readable readable output} is required, although the result of reading from this instance is always as if nothing
* was written.
*
* @return an {@code Output} instance that writes nowhere.
*/
static Output.Readable nowhere()
{
return Nowhere.OUTPUT;
}
/**
* Wrap an {@link Output} in another instance that prepends a line number to every line written.
*
* @param output the wrapped output, where numbered lines will be written to.
* @return an {@code Output} instance that prepends lines with line numbers.
*/
static Output lineNumbers( Output output )
{
return new LineNumberingOutput( output );
}
/**
* Combine multiple {@code Output} instances to one instance that writes to all combined instances.
*
* @param output the output instances to combine and write all output to.
* @return an {@code Output} instance that writes to all the supplied instances.
*/
static Output multiplex( Output... output )
{
if ( output == null || output.length == 0 )
{
return nowhere();
}
if ( output.length == 1 )
{
return output[0];
}
Set