try-monad

A Java Try Monad

Лицензия

Лицензия

Группа

Группа

com.jecklgamis
Идентификатор

Идентификатор

try-monad
Последняя версия

Последняя версия

1.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

try-monad
A Java Try Monad
Ссылка на сайт

Ссылка на сайт

http://github.com/jecklgamis/try-monad
Система контроля версий

Система контроля версий

http://github.com/jecklgamis/try-monad/tree/master

Скачать try-monad

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/com.jecklgamis/try-monad/ -->
<dependency>
    <groupId>com.jecklgamis</groupId>
    <artifactId>try-monad</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.jecklgamis/try-monad/
implementation 'com.jecklgamis:try-monad:1.0'
// https://jarcasting.com/artifacts/com.jecklgamis/try-monad/
implementation ("com.jecklgamis:try-monad:1.0")
'com.jecklgamis:try-monad:jar:1.0'
<dependency org="com.jecklgamis" name="try-monad" rev="1.0">
  <artifact name="try-monad" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.jecklgamis', module='try-monad', version='1.0')
)
libraryDependencies += "com.jecklgamis" % "try-monad" % "1.0"
[com.jecklgamis/try-monad "1.0"]

Зависимости

test (1)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.12

Модули Проекта

Данный проект не имеет модулей.

try-monad Build Status

About

Inspired by scala.util.Try, this is small library that provides an abstraction of result of a function application.

Getting Started

Maven
<dependency>
    <groupId>com.jecklgamis</groupId>
    <artifactId>try-monad</artifactId>
    <version>1.0</version>
</dependency>
Gradle
compile 'com.jecklgamis:try-monad:1.0'
Apache Buildr
'com.jecklgamis:try-monad:jar:1.0'
Groovy Grapes
@Grapes(
@Grab(group='com.jecklgamis', module='try-monad', version='1.0')
)
Leiningen
[com.jecklgamis/try-monad "1.0"]

A Primer On Using Try

Below is an example util function to check socket connection to a specified host.

import java.io.IOException;
import java.net.Socket;

import static com.jecklgamis.util.TryFactory.attempt;

public class ExampleUsage {

    public static boolean canConnect(String host, int port) {
        return attempt(() -> new Socket(host, port)).map((s) -> s.isConnected()).getOrElse(false);
    }

    public static void main(String[] args) {
        System.out.println(canConnect("localhost", 80));
    }
}

The canConnect function uses TryFactory.attempt (similar to try) to invoke a function that returns a socket instance (() → new Socket). It will then return an instance of Try , specifically a Success if there is no exception thrown, or Failure otherwise.

If the call is successful, the function passed to map (i.e. (s) → s.isConnected()) will be invoked. The s parameter in the expression is the result of function in attempt (a socket instance). If attempt fails (attempt returned a Failure), map will not be invoked and simply returns the Failure returned by attempt.

The last call in the chain is getOrElse(false), which will return a boolean depending on the output of map. If the map returns a Success (no exception thrown in (s)→ s.isConnected), `getOrElse will simply return the value of the function passed to map. Otherwise, getOrElse will return the given value, which in this case false.

Версии библиотеки

Версия
1.0