All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jenetics.jpx.TrackSegment Maven / Gradle / Ivy

/*
 * Java GPX Library (jpx-1.0.0).
 * Copyright (c) 2016-2016 Franz Wilhelmstötter
 *
 * 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.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package io.jenetics.jpx;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static io.jenetics.jpx.Lists.immutable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

/**
 * A Track Segment holds a list of Track Points which are logically connected in
 * order. To represent a single GPS track where GPS reception was lost, or the
 * GPS receiver was turned off, start a new Track Segment for each continuous
 * span of track data.
 *
 * @author Franz Wilhelmstötter
 * @version 1.0
 * @since 1.0
 */
public final class TrackSegment implements Iterable, Serializable {

	private static final long serialVersionUID = 1L;

	private final List _points;

	/**
	 * Create a new track-segment with the given points.
	 *
	 * @param points the points of the track-segment
	 */
	private TrackSegment(final List points) {
		_points = immutable(points);
	}

	/**
	 * Return the track-points of this segment.
	 *
	 * @return the track-points of this segment
	 */
	public List getPoints() {
		return _points;
	}

	/**
	 * Return a stream of {@link WayPoint} objects this track-segments contains.
	 *
	 * @return a stream of {@link WayPoint} objects this track-segment contains
	 */
	public Stream points() {
		return _points.stream();
	}

	/**
	 * Return {@code true} if {@code this} track-segment doesn't contain any
	 * track-point.
	 *
	 * @return {@code true} if {@code this} track-segment is empty, {@code false}
	 *         otherwise
	 */
	public boolean isEmpty() {
		return _points.isEmpty();
	}

	@Override
	public Iterator iterator() {
		return _points.iterator();
	}

	@Override
	public int hashCode() {
		return _points.hashCode();
	}

	@Override
	public boolean equals(final Object obj) {
		return obj instanceof TrackSegment &&
			((TrackSegment)obj)._points.equals(_points);
	}

	@Override
	public String toString() {
		return format("TrackSegment[points=%s]", _points.size());
	}

	/**
	 * Builder class for creating immutable {@code TrackSegment} objects.
	 * 

* Creating a {@code TrackSegment} object with 3 track-points: *

{@code
	 * final TrackSegment segment = TrackSegment.builder()
	 *     .addPoint(p -> p.lat(48.2081743).lon(16.3738189).ele(160))
	 *     .addPoint(p -> p.lat(48.2081743).lon(16.3738189).ele(161))
	 *     .addPoint(p -> p.lat(48.2081743).lon(16.3738189).ele(162))))
	 *     .build();
	 * }
*/ public static final class Builder { private List _points; private Builder() { } /** * Set the way-points fo the track segment. * * @param points the track-segment points * @return {@code this} {@code Builder} for method chaining */ public Builder points(final List points) { _points = points; return this; } /** * Add a way-point to the track-segment. * * @param point the segment way-point * @return {@code this} {@code Builder} for method chaining * @throws NullPointerException if the given {@code href} is {@code null} */ public Builder addPoint(final WayPoint point) { if (_points == null) { _points = new ArrayList<>(); } _points.add(requireNonNull(point)); return this; } /** * Add a way-point to the track-segment, via the given way-point builder. *

* Creating a {@code TrackSegment} object with 3 track-points: *

{@code
		 * final TrackSegment segment = TrackSegment.builder()
		 *     .addPoint(p -> p.lat(48.2081743).lon(16.3738189).ele(160))
		 *     .addPoint(p -> p.lat(48.2081743).lon(16.3738189).ele(161))
		 *     .addPoint(p -> p.lat(48.2081743).lon(16.3738189).ele(162))))
		 *     .build();
		 * }
* * @param point the segment way-point builder * @return {@code this} {@code Builder} for method chaining * @throws NullPointerException if the given {@code href} is {@code null} */ public Builder addPoint(final Consumer point) { final WayPoint.Builder builder = WayPoint.builder(); point.accept(builder); return addPoint(builder.build()); } /** * Create a new track-segment from the current builder state. * * @return a new track-segment from the current builder state */ public TrackSegment build() { return new TrackSegment(_points); } } /** * Create a new track-segment builder. * * @return a new track-segment builder */ public static Builder builder() { return new Builder(); } /* ************************************************************************* * Static object creation methods * ************************************************************************/ /** * Create a new track-segment with the given points. * * @param points the points of the track-segment * @return a new track-segment with the given points * @throws NullPointerException if the given {@code points} sequence is * {@code null} */ public static TrackSegment of(final List points) { return new TrackSegment(points); } /* ************************************************************************* * XML stream object serialization * ************************************************************************/ /** * Writes this {@code Link} object to the given XML stream {@code writer}. * * @param writer the XML data sink * @throws XMLStreamException if an error occurs */ void write(final XMLStreamWriter writer) throws XMLStreamException { final XMLWriter xml = new XMLWriter(writer); xml.write("trkseg", xml.elems(_points, (p, w) -> p.write("trkpt", w)) ); } @SuppressWarnings("unchecked") static XMLReader reader() { final Function creator = a -> TrackSegment.of( (List)a[0] ); return XMLReader.of(creator, "trkseg", XMLReader.ofList(WayPoint.reader("trkpt")) ); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy