public void testBasic() throws Exception { if (!Dpkg.available()) { return; } DebPackagingFormat packagingFormat = (DebPackagingFormat) lookup(PackagingFormat.ROLE, "deb"); FileObject dpkgTest = VFS.getManager().resolveFile(getTestPath("target/deb-test")); FileObject packageRoot = dpkgTest.resolveFile("root"); File packageFile = VfsUtil.asFile(dpkgTest.resolveFile("file.deb")); PackageVersion version = packageVersion("1.0", "123", false, some("1")); PackageParameters parameters = packageParameters( "mygroup", "myartifact", version, "id", "default-name", Option.<java.lang.String>none(), EMPTY, EMPTY) .contact("Kurt Cobain") .architecture("all"); List<String> nil = List.nil(); packagingFormat .start() .parameters(parameters) .debParameters(Option.<String>none(), some("devel"), false, nil, nil, nil, nil, nil, nil) .debug(true) .workingDirectory(packageRoot) .packageToFile(packageFile, ScriptUtil.Strategy.SINGLE); assertTrue(packageFile.canRead()); }
/** @author <a href="mailto:[email protected]">Trygve Laugstøl</a> */ public class FileAttributes { public final Option<String> user; public final Option<String> group; public final Option<UnixFileMode> mode; public final List<String> tags; public static final Show<FileAttributes> singleLineShow = Show.anyShow(); /** A file object with all none fields. Use this when creating template objects. */ public static final FileAttributes EMPTY = new FileAttributes( Option.<String>none(), Option.<String>none(), Option.<UnixFileMode>none(), List.<String>nil()); public FileAttributes(Option<String> user, Option<String> group, Option<UnixFileMode> mode) { this(user, group, mode, List.<String>nil()); } public FileAttributes( Option<String> user, Option<String> group, Option<UnixFileMode> mode, List<String> tags) { validateNotNull(user, group, mode, tags); this.user = user; this.group = group; this.mode = mode; this.tags = tags; } public FileAttributes user(String user) { return new FileAttributes(fromNull(user), group, mode, tags); } public FileAttributes user(Option<String> user) { return new FileAttributes(user, group, mode, tags); } public FileAttributes group(String group) { return new FileAttributes(user, fromNull(group), mode, tags); } public FileAttributes group(Option<String> group) { return new FileAttributes(user, group, mode, tags); } public FileAttributes mode(UnixFileMode mode) { return new FileAttributes(user, group, fromNull(mode), tags); } public FileAttributes mode(Option<UnixFileMode> mode) { return new FileAttributes(user, group, mode, tags); } public FileAttributes addTag(String tag) { return new FileAttributes(user, group, mode, tags.append(single(tag))); } public FileAttributes tags(List<String> tags) { return new FileAttributes(user, group, mode, this.tags.append(tags)); } // ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- public FileAttributes useAsDefaultsFor(FileAttributes other) { return new FileAttributes( other.user.orElse(user), other.group.orElse(group), other.mode.orElse(mode), other.tags); } // ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- public static final F2<FileAttributes, FileAttributes, FileAttributes> useAsDefaultsFor = new F2<FileAttributes, FileAttributes, FileAttributes>() { public FileAttributes f(FileAttributes defaults, FileAttributes other) { return defaults.useAsDefaultsFor(other); } }; public static final F<FileAttributes, Option<String>> userF = new F<FileAttributes, Option<String>>() { public Option<String> f(FileAttributes attributes) { return attributes.user; } }; public static final F<FileAttributes, Option<String>> groupF = new F<FileAttributes, Option<String>>() { public Option<String> f(FileAttributes attributes) { return attributes.group; } }; public static final F<FileAttributes, Option<UnixFileMode>> modeF = new F<FileAttributes, Option<UnixFileMode>>() { public Option<UnixFileMode> f(FileAttributes attributes) { return attributes.mode; } }; public static final F<FileAttributes, List<String>> tagsF = new F<FileAttributes, List<String>>() { public List<String> f(FileAttributes attributes) { return attributes.tags; } }; // ----------------------------------------------------------------------- // Object Overrides // ----------------------------------------------------------------------- public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } FileAttributes that = (FileAttributes) o; return optionEquals(user, that.user) && optionEquals(group, that.group) && optionEquals(mode, that.mode); } public String toString() { return "user="******"<not set>") + ", " + "group=" + group.orSome("<not set>") + ", " + "mode=" + mode.map(showLong).orSome("<not set>"); } }
/** * Returns <code>None</code> if this projection has no value or if the given predicate <code>p * </code> does not hold for the value, otherwise, returns a left in <code>Some</code>. * * @param f The predicate function to test on this projection's value. * @return <code>None</code> if this projection has no value or if the given predicate <code>p * </code> does not hold for the value, otherwise, returns a left in <code>Some</code>. */ public <X> Option<Either<X, B>> filter(final Func<B, Boolean> f) { return isRight() ? f.f(value()) ? Option.<Either<X, B>>some(new Right<X, B>(value())) : Option.<Either<X, B>>none() : Option.<Either<X, B>>none(); }
/** * Returns <code>None</code> if this projection has no value or if the given predicate <code>p * </code> does not hold for the value, otherwise, returns a right in <code>Some</code>. * * @param f The predicate function to test on this projection's value. * @return <code>None</code> if this projection has no value or if the given predicate <code>p * </code> does not hold for the value, otherwise, returns a right in <code>Some</code>. */ public <X> Option<Either<A, X>> filter(final Func<A, Boolean> f) { return isLeft() ? f.f(value()) ? Option.<Either<A, X>>some(new Left<A, X>(value())) : Option.<Either<A, X>>none() : Option.<Either<A, X>>none(); }
@Path("/events/{eventName}/icons/labels/{label}.png") @GET @Produces("image/png") public Response getLabelIcon( @PathParam("eventName") final String eventName, @PathParam("label") final String label) { OperationResult<Event> eventResult = incogito.getEventByName(eventName); if (!eventResult.isOk()) { return toJsr311(eventResult); } Option<File> fileOption = eventResult.value().labelMap.get(label).map(Label.iconFile_); if (!fileOption.isSome()) { return toJsr311(OperationResult.<Object>notFound("No icon for label '" + label + "'.")); } // TODO: How about some caching here? Option<byte[]> bytes = join( fileOption .map(IO.<byte[]>runFileInputStream_().f(IO.ByteArrays.streamToByteArray)) .map(compose(P1.<Option<byte[]>>__1(), Callables.<byte[]>option()))); return toJsr311(fromOption(bytes, "Unable to read label file."), cacheForOneHourCacheControl); }
/** * Get the value of the AD parameter by type for the activity * * @param element * @param parameterType * @return */ @SuppressWarnings("unchecked") public static <T> Option<T> getParameterObjectByType(EPlanElement element, EClass parameterType) { EObject data = element.getData(); EList<EStructuralFeature> structuralFeatures = data.eClass().getEStructuralFeatures(); for (int i = 0; i < structuralFeatures.size(); i++) { final EStructuralFeature feature = structuralFeatures.get(i); if (parameterType == feature.getEType()) return Option.fromNull((T) data.eGet(feature)); } return Option.none(); }
public String toString() { return "user="******"<not set>") + ", " + "group=" + group.orSome("<not set>") + ", " + "mode=" + mode.map(showLong).orSome("<not set>"); }
/** * Invariant functor map over this enumerator. * * @param f The covariant map. * @param g The contra-variant map. * @return An enumerator after the given functions are applied. */ public <B> Enumerator<B> xmap(final F<A, B> f, final F<B, A> g) { final F<Option<A>, Option<B>> of = o -> o.map(f); return enumerator( compose(compose(of, successor), g), compose(compose(of, predecessor), g), max.map(f), min.map(f), order.contramap(g), compose(compose(Function.<Long, Option<A>, Option<B>>compose().f(of), plus), g)); }
/** * Binds the given function across the element of this optional value and the given optional value * with a final join. * * @param ob A given optional value to bind the given function with. * @param oc A given optional value to bind the given function with. * @param od A given optional value to bind the given function with. * @param f The function to apply to the element of this optional value and the given optional * value. * @return A new optional value after performing the map, then final join. */ public <B, C, D, E> Option<E> bind( final Option<B> ob, final Option<C> oc, final Option<D> od, final F<A, F<B, F<C, F<D, E>>>> f) { return od.apply(bind(ob, oc, f)); }
@GET @Produces(MediaType.APPLICATION_JSON) public Response invoices(@Context HttpServletRequest request) throws SQLException, IOException { authService.guardAuthenticatedUser(request); try (Connection c = dataSource.getConnection()) { List<UUID> ids = registrationsSqlMapper.all(c).map(x -> x.fst()); List<RegistrationsSqlMapper.Registration> all = Option.somes(ids.traverseIO(ioify(c)).run()); JsonRootNode overview = object( field( "received", array( all.filter(x -> x.tuple.state == RegistrationState.RECEIVED) .map(RegistrationsRestService::json))), field( "invoicing", array( all.filter(x -> x.tuple.state == RegistrationState.INVOICING) .map(RegistrationsRestService::json))), field( "paid", array( all.filter(x -> x.tuple.state == RegistrationState.PAID) .map(RegistrationsRestService::json)))); return Response.ok(ArgoUtils.format(overview)).build(); } }
/** * Binds the given function across the element of this optional value and the given optional value * with a final join. * * @param ob A given optional value to bind the given function with. * @param oc A given optional value to bind the given function with. * @param od A given optional value to bind the given function with. * @param oe A given optional value to bind the given function with. * @param f The function to apply to the element of this optional value and the given optional * value. * @return A new optional value after performing the map, then final join. */ public <B, C, D, E, F$> Option<F$> bind( final Option<B> ob, final Option<C> oc, final Option<D> od, final Option<E> oe, final F<A, F<B, F<C, F<D, F<E, F$>>>>> f) { return oe.apply(bind(ob, oc, od, f)); }
/** * Binds the given function across the element of this optional value and the given optional value * with a final join. * * @param ob A given optional value to bind the given function with. * @param oc A given optional value to bind the given function with. * @param od A given optional value to bind the given function with. * @param oe A given optional value to bind the given function with. * @param of A given optional value to bind the given function with. * @param og A given optional value to bind the given function with. * @param f The function to apply to the element of this optional value and the given optional * value. * @return A new optional value after performing the map, then final join. */ public <B, C, D, E, F$, G, H> Option<H> bind( final Option<B> ob, final Option<C> oc, final Option<D> od, final Option<E> oe, final Option<F$> of, final Option<G> og, final F<A, F<B, F<C, F<D, F<E, F<F$, F<G, H>>>>>>> f) { return og.apply(bind(ob, oc, od, oe, of, f)); }
private int sub() throws Exception { Array<P2<String, Option<UUID>>> parse = outvoicePaidClient.parse(outvoicePaidClient.get()); int i = 0; for (P2<String, Option<UUID>> p : parse) { RegistrationsSqlMapper.Registration registration; try (Connection c = dataSource.getConnection()) { c.setAutoCommit(false); Option<UUID> apiClientRef = p._2(); if (apiClientRef.isNone()) continue; Option<UUID> registrationId = registrationsSqlMapper.invoiceReferenceToRegistrationId(c, apiClientRef.some()); if (registrationId.isNone()) continue; registration = registrationsSqlMapper.one(c, registrationId.some()).some(); } if (registration.tuple.state != RegistrationState.INVOICING) continue; markAsPaidService.markAsPaid(registration.id); i++; } return i; }
/** * Construct an enumerator. The <code>plus</code> function is derived from the <code>successor * </code> and <code>predecessor</code>. * * @param successor The successor function. * @param predecessor The predecessor function. * @param max The potential maximum value. * @param min The potential minimum value. * @param order The ordering for the type. * @return An enumerator with the given values. */ public static <A> Enumerator<A> enumerator( final F<A, Option<A>> successor, final F<A, Option<A>> predecessor, final Option<A> max, final Option<A> min, final Ord<A> order) { return new Enumerator<A>( successor, predecessor, max, min, order, curry( (a, l) -> { if (l == 0L) return some(a); else if (l < 0L) { A aa = a; for (long x = l; x < 0; x++) { final Option<A> s = predecessor.f(aa); if (s.isNone()) return none(); else aa = s.some(); } return some(aa); } else { A aa = a; for (long x = l; x > 0; x--) { final Option<A> s = successor.f(aa); if (s.isNone()) return none(); else aa = s.some(); } return some(aa); } })); }
@Path("/events/{eventName}/sessions/{sessionId}/session-interest") @POST public Response setSessionInterest( @Context final SecurityContext securityContext, @PathParam("eventName") final String eventName, @PathParam("sessionId") final String sessionId, String payload) { Option<String> userName = getUserName.f(securityContext); if (userName.isNone()) { return Response.status(Status.UNAUTHORIZED).build(); } OperationResult<User> result = incogito.setInterestLevel( userName.some(), eventName, new SessionId(sessionId), InterestLevel.valueOf(payload)); return this.<User>defaultResponsePatternMatcher() .add(OkOperationResult.class, this.<OperationResult<User>>created()) .match(result); }
private void onEvent(NexusDao dao, HasNexusEvent e) throws SQLException { NexusEvent event = e.event; String repositoryId = event.guid.replaceAll( "^" + quote(server.url.toASCIIString()) + "/content/repositories/([-a-zA-Z0-9]*)/.*", "$1"); if (repositoryId.length() == 0) { return; } Option<NexusRepositoryDto> r = dao.findRepository(server.uuid, repositoryId); if (r.isNone()) { return; } NexusRepositoryDto repository = r.some(); Option<ArtifactDto> a = dao.findArtifact(repository.uuid, event.artifactId); UUID uuid; if (a.isNone()) { System.out.println("New artifact: " + event.artifactId); uuid = dao.insertArtifact(repository.uuid, event.artifactId); } else { ArtifactDto artifact = a.some(); // System.out.println("Updated artifact: " + event.artifactId); uuid = artifact.uuid; } if (e instanceof NewSnapshotEvent) { NewSnapshotEvent newSnapshotEvent = (NewSnapshotEvent) e; dao.insertNewSnapshotEvent( uuid, event.guid, event.creator, event.date, newSnapshotEvent.snapshotTimestamp, newSnapshotEvent.buildNumber, newSnapshotEvent.url.toASCIIString()); } else if (e instanceof NewReleaseEvent) { NewReleaseEvent nre = (NewReleaseEvent) e; dao.insertNewReleaseEvent( uuid, event.guid, event.creator, event.date, nre.url.toASCIIString()); } else { System.out.println("Unknown event type: " + event.getClass().getName()); } }
@POST @Path("/mark-as-printed") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response markAsPrinted(@Context HttpServletRequest request, String body) throws Exception { authService.guardAuthenticatedUser(request); int i = 0; for (UUID uuid : registrationListRequest(body)) { try (Connection c = dataSource.getConnection()) { registrationsSqlMapper.replacePrintedNametag(c, uuid, Option.some(new PrintedNametag())); } i++; } return Response.ok( ArgoUtils.format( Result.success(String.format("Markerade %s namnbrickor som utskrivna.", i)))) .build(); }
public MultiEObject(List<? extends EObject> eObjects, MultiItemPropertySource source) { this(eObjects, source, Option.<String>none(), Option.<Image>none()); }
/** * Abstracts over a type that may have a successor and/or predecessor value. This implies ordering * for that type. A user may construct an enumerator with an optimised version for <code>plus</code> * , otherwise a default is implemented using the given successor/predecessor implementations. * * <p>For any enumerator e, the following laws must satisfy: * * <ul> * <li>forall a. e.successor(a).forall(\t -> e.predecessor(t).forall(\z -> z == a)) * <li>forall a. e.predecessor(a).forall(\t -> e.successor(t).forall(\z -> z == a)) * <li>e.max().forall(\t -> e.successor(t).isNone) * <li>e.min().forall(\t -> e.predecessor(t).isNone) * <li>forall a n. e.plus(a, 0) == Some(a) * <li>forall a n | n > 0. e.plus(a, n) == e.plus(a, n - 1) * <li>forall a n | n < 0. e.plus(a, n) == e.plus(a, n + 1) * </ul> * * @version %build.number% */ public final class Enumerator<A> { private final F<A, Option<A>> successor; private final F<A, Option<A>> predecessor; private final Option<A> max; private final Option<A> min; private final Ord<A> order; private final F<A, F<Long, Option<A>>> plus; private Enumerator( final F<A, Option<A>> successor, final F<A, Option<A>> predecessor, final Option<A> max, final Option<A> min, final Ord<A> order, final F<A, F<Long, Option<A>>> plus) { this.successor = successor; this.predecessor = predecessor; this.max = max; this.min = min; this.order = order; this.plus = plus; } /** * Returns the potential successor of a value for this enumerator in curried form. * * @return The potential successor of a value for this enumerator in curried form. */ public F<A, Option<A>> successor() { return successor; } /** * Returns the potential successor of a value for this enumerator. * * @param a The value to return the successor of. * @return The potential successor of a value for this enumerator. */ public Option<A> successor(final A a) { return successor.f(a); } /** * Returns the potential predecessor of a value for this enumerator in curried form. * * @return The potential predecessor of a value for this enumerator in curried form. */ public F<A, Option<A>> predecessor() { return predecessor; } /** * Returns the potential predecessor of a value for this enumerator. * * @param a The value to return the predecessor of. * @return The potential predecessor of a value for this enumerator. */ public Option<A> predecessor(final A a) { return predecessor.f(a); } /** * Returns the maximum value for this enumerator if there is one. * * @return The maximum value for this enumerator if there is one. */ public Option<A> max() { return max; } /** * Returns the minimum value for this enumerator if there is one. * * @return The minimum value for this enumerator if there is one. */ public Option<A> min() { return min; } /** * Returns a function that moves a value along the enumerator a given number of times. * * @return A function that moves a value along the enumerator a given number of times. */ public F<A, F<Long, Option<A>>> plus() { return plus; } /** * Returns a function that moves a value along the enumerator a given number of times. * * @param a The value to begin moving along from. * @return A function that moves a value along the enumerator a given number of times. */ public F<Long, Option<A>> plus(final A a) { return plus.f(a); } /** * Returns a function that moves a value along the enumerator a given number of times. * * @param l The number of times to move along the enumerator. * @return A function that moves a value along the enumerator a given number of times. */ public F<A, Option<A>> plus(final long l) { return flip(plus).f(l); } /** * Moves a value along the enumerator a given number of times. * * @param a The value to begin moving along from. * @param l The number of times to move along the enumerator. * @return A potential value after having moved the given number of times. */ public Option<A> plus(final A a, final long l) { return plus.f(a).f(l); } /** * Returns the ordering for the enumerator. * * @return The ordering for the enumerator. */ public Ord<A> order() { return order; } /** * Invariant functor map over this enumerator. * * @param f The covariant map. * @param g The contra-variant map. * @return An enumerator after the given functions are applied. */ public <B> Enumerator<B> xmap(final F<A, B> f, final F<B, A> g) { final F<Option<A>, Option<B>> of = o -> o.map(f); return enumerator( compose(compose(of, successor), g), compose(compose(of, predecessor), g), max.map(f), min.map(f), order.contramap(g), compose(compose(Function.<Long, Option<A>, Option<B>>compose().f(of), plus), g)); } /** * Returns a stream of the values from this enumerator, starting at the given value, counting up. * * @param a A value at which to begin the stream. * @return a stream of the values from this enumerator, starting at the given value, counting up. */ public Stream<A> toStream(final A a) { final F<A, A> id = identity(); return Stream.fromFunction(this, id, a); } /** * Create a new enumerator with the given minimum value. * * @param min A minimum value. * @return A new enumerator identical to this one, but with the given minimum value. */ public Enumerator<A> setMin(final Option<A> min) { return enumerator(successor, predecessor, max, min, order, plus); } /** * Create a new enumerator with the given maximum value. * * @param max A maximum value. * @return A new enumerator identical to this one, but with the given maximum value. */ public Enumerator<A> setMax(final Option<A> max) { return enumerator(successor, predecessor, max, min, order, plus); } /** * Construct an enumerator. ` * * @param successor The successor function. * @param predecessor The predecessor function. * @param max The potential maximum value. * @param min The potential minimum value. * @param order The ordering for the type. * @param plus The function to move the enumeration a given number of times. This may be supplied * for a performance enhancement for certain types. * @return An enumerator with the given values. */ public static <A> Enumerator<A> enumerator( final F<A, Option<A>> successor, final F<A, Option<A>> predecessor, final Option<A> max, final Option<A> min, final Ord<A> order, final F<A, F<Long, Option<A>>> plus) { return new Enumerator<A>(successor, predecessor, max, min, order, plus); } /** * Construct an enumerator. The <code>plus</code> function is derived from the <code>successor * </code> and <code>predecessor</code>. * * @param successor The successor function. * @param predecessor The predecessor function. * @param max The potential maximum value. * @param min The potential minimum value. * @param order The ordering for the type. * @return An enumerator with the given values. */ public static <A> Enumerator<A> enumerator( final F<A, Option<A>> successor, final F<A, Option<A>> predecessor, final Option<A> max, final Option<A> min, final Ord<A> order) { return new Enumerator<A>( successor, predecessor, max, min, order, curry( (a, l) -> { if (l == 0L) return some(a); else if (l < 0L) { A aa = a; for (long x = l; x < 0; x++) { final Option<A> s = predecessor.f(aa); if (s.isNone()) return none(); else aa = s.some(); } return some(aa); } else { A aa = a; for (long x = l; x > 0; x--) { final Option<A> s = successor.f(aa); if (s.isNone()) return none(); else aa = s.some(); } return some(aa); } })); } /** An enumerator for <code>boolean</code>. */ public static final Enumerator<Boolean> booleanEnumerator = enumerator( b -> b ? Option.<Boolean>none() : some(true), b -> b ? some(false) : Option.<Boolean>none(), some(true), some(false), booleanOrd); /** An enumerator for <code>byte</code>. */ public static final Enumerator<Byte> byteEnumerator = enumerator( b -> b == Byte.MAX_VALUE ? Option.<Byte>none() : some((byte) (b + 1)), b -> b == Byte.MIN_VALUE ? Option.<Byte>none() : some((byte) (b - 1)), some(Byte.MAX_VALUE), some(Byte.MIN_VALUE), byteOrd); /** An enumerator for <code>char</code>. */ public static final Enumerator<Character> charEnumerator = enumerator( c -> c == Character.MAX_VALUE ? Option.<Character>none() : some((char) (c + 1)), c -> c == Character.MIN_VALUE ? Option.<Character>none() : some((char) (c - 1)), some(Character.MAX_VALUE), some(Character.MIN_VALUE), charOrd); /** An enumerator for <code>double</code>. */ public static final Enumerator<Double> doubleEnumerator = enumerator( d -> d == Double.MAX_VALUE ? Option.<Double>none() : some(d + 1D), d -> d == Double.MIN_VALUE ? Option.<Double>none() : some(d - 1D), some(Double.MAX_VALUE), some(Double.MIN_VALUE), doubleOrd); /** An enumerator for <code>float</code>. */ public static final Enumerator<Float> floatEnumerator = enumerator( f -> f == Float.MAX_VALUE ? Option.<Float>none() : some(f + 1F), f -> f == Float.MIN_VALUE ? Option.<Float>none() : some(f - 1F), some(Float.MAX_VALUE), some(Float.MIN_VALUE), floatOrd); /** An enumerator for <code>int</code>. */ public static final Enumerator<Integer> intEnumerator = enumerator( i -> i == Integer.MAX_VALUE ? Option.<Integer>none() : some(i + 1), i -> i == Integer.MIN_VALUE ? Option.<Integer>none() : some(i - 1), some(Integer.MAX_VALUE), some(Integer.MIN_VALUE), intOrd); /** An enumerator for <code>BigInteger</code>. */ public static final Enumerator<BigInteger> bigintEnumerator = enumerator( i -> some(i.add(BigInteger.ONE)), i -> some(i.subtract(BigInteger.ONE)), Option.<BigInteger>none(), Option.<BigInteger>none(), bigintOrd, curry((i, l) -> some(i.add(BigInteger.valueOf(l))))); /** An enumerator for <code>BigDecimal</code>. */ public static final Enumerator<BigDecimal> bigdecimalEnumerator = enumerator( i -> some(i.add(BigDecimal.ONE)), i -> some(i.subtract(BigDecimal.ONE)), Option.<BigDecimal>none(), Option.<BigDecimal>none(), bigdecimalOrd, curry((d, l) -> some(d.add(BigDecimal.valueOf(l))))); /** An enumerator for <code>long</code>. */ public static final Enumerator<Long> longEnumerator = enumerator( i -> i == Long.MAX_VALUE ? Option.<Long>none() : some(i + 1L), i -> i == Long.MIN_VALUE ? Option.<Long>none() : some(i - 1L), some(Long.MAX_VALUE), some(Long.MIN_VALUE), longOrd); /** An enumerator for <code>short</code>. */ public static final Enumerator<Short> shortEnumerator = enumerator( i -> i == Short.MAX_VALUE ? Option.<Short>none() : some((short) (i + 1)), i -> i == Short.MIN_VALUE ? Option.<Short>none() : some((short) (i - 1)), some(Short.MAX_VALUE), some(Short.MIN_VALUE), shortOrd); /** An enumerator for <code>Ordering</code>. */ public static final Enumerator<Ordering> orderingEnumerator = enumerator( o -> o == LT ? some(EQ) : o == EQ ? some(GT) : Option.<Ordering>none(), o -> o == GT ? some(EQ) : o == EQ ? some(LT) : Option.<Ordering>none(), some(GT), some(LT), orderingOrd); /** An enumerator for <code>Natural</code> */ public static final Enumerator<Natural> naturalEnumerator = enumerator( n -> Option.some(n.succ()), n -> n.pred(), Option.<Natural>none(), some(Natural.ZERO), naturalOrd, curry( (n, l) -> some(n).apply(Natural.natural(l).map(Function.curry((n1, n2) -> n1.add(n2)))))); }
/** * Binds the given function across the element of this optional value with a final join. * * @param f The function to apply to the element of this optional value. * @return A new optional value after performing the map, then final join. */ public <B> Option<B> bind(final F<A, Option<B>> f) { return isSome() ? f.f(some()) : Option.<B>none(); }
/** * Binds the given function across the element of this optional value and the given optional value * with a final join. * * @param ob A given optional value to bind the given function with. * @param f The function to apply to the element of this optional value and the given optional * value. * @return A new optional value after performing the map, then final join. */ public <B, C> Option<C> bind(final Option<B> ob, final F<A, F<B, C>> f) { return ob.apply(map(f)); }
public static <T> boolean contains(List<T> l, F<T, Boolean> f) { Option<T> o = l.find(f); return o.isSome(); }
public Option<String> f(SecurityContext securityContext) { return securityContext.getUserPrincipal() == null ? Option.<String>none() : fromString(securityContext.getUserPrincipal().getName()); }
/** * Binds the given function across the element of this optional value and the given optional value * with a final join. * * @param ob A given optional value to bind the given function with. * @param oc A given optional value to bind the given function with. * @param f The function to apply to the element of this optional value and the given optional * value. * @return A new optional value after performing the map, then final join. */ public <B, C, D> Option<D> bind( final Option<B> ob, final Option<C> oc, final F<A, F<B, F<C, D>>> f) { return oc.apply(bind(ob, f)); }
/** * Turns an unsafe nullable value into a safe optional value. If <code>t == null</code> then * return none, otherwise, return the given value in some. * * @param t The unsafe nullable value. * @return If <code>t == null</code> then return it in some, otherwise, return none. */ public static <T> Option<T> fromNull(final T t) { return t == null ? Option.<T>none() : some(t); }
/** * Returns an optional value that has a value of the given argument, if the given predicate holds * on that argument, otherwise, returns no value. * * @param f The predicate to test on the given argument. * @param a The argument to test the predicate on and potentially use as the value of the returned * optional value. * @return an optional value that has a value of the given argument, if the given predicate holds * on that argument, otherwise, returns no value. */ public static <A> Option<A> iif(final F<A, Boolean> f, final A a) { return f.f(a) ? some(a) : Option.<A>none(); }
/** * Returns a potential non-empty list from the given list. A non-value is returned if the given * list is empty. * * @param as The list to construct a potential non-empty list with. * @return A potential non-empty list from the given list. */ public static <A> Option<NonEmptyList<A>> fromList(final List<A> as) { return as.isEmpty() ? Option.<NonEmptyList<A>>none() : some(nel(as.head(), as.tail())); }
/** * Returns this projection's value in <code>Some</code> if it exists, otherwise <code>None * </code>. * * @return This projection's value in <code>Some</code> if it exists, otherwise <code>None * </code>. */ public Option<B> toOption() { return isRight() ? some(value()) : Option.<B>none(); }
/** * Returns this projection's value in <code>Some</code> if it exists, otherwise <code>None * </code>. * * @return This projection's value in <code>Some</code> if it exists, otherwise <code>None * </code>. */ public Option<A> toOption() { return isLeft() ? some(value()) : Option.<A>none(); }
/** * Filters elements from this optional value by returning only elements which produce <code>true * </code> when the given function is applied to them. * * @param f The predicate function to filter on. * @return A new optional value whose value matches the given predicate if it has one. */ public Option<A> filter(final F<A, Boolean> f) { return isSome() ? f.f(some()) ? this : Option.<A>none() : Option.<A>none(); }