本文整理匯總了Java中fj.data.Either類的典型用法代碼示例。如果您正苦於以下問題:Java Either類的具體用法?Java Either怎麽用?Java Either使用的例子?那麽恭喜您, 這裏精選的類代碼示例或許可以為您提供幫助。
Either類屬於fj.data包,在下文中一共展示了Either類的36個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於我們的係統推薦出更棒的Java代碼示例。
示例1: set
點讚 3
import fj.data.Either; //導入依賴的package包/類
private void set(final String key, Option newValue, final WriteOptions writeOpts) throws CoreException {
final Option oldValue = get(key);
newValue = newValue.orElse(getDefault(key));
if (optionEqual(stringEqual).eq(newValue, oldValue))
return;
if (newValue.isSome())
props._1().put(key, newValue.some());
else
props._1().remove(key);
final ProjectPropertyEvent event = new ProjectPropertyEvent(resource, key, newValue);
switch (writeOpts) {
case DONT_WRITE:
// do nothing
break;
case WRITE_SYNC:
writeProp(event);
break;
case WRITE_ASYNC:
final Either left = left(event);
writeQueue._1().add(left);
break;
}
}
開發者ID:nasa,項目名稱:OpenSPIFe,代碼行數:27,
示例2: readCert
點讚 3
import fj.data.Either; //導入依賴的package包/類
/**
* Reads a certificate from our .crt file format...
*/
private final IO> readCert(final File f) {
return new IO>() {
public Either run() {
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
String cert = "";
boolean readingCert = false;
while ((line = br.readLine()) != null) {
if (!readingCert && line.contains("----")) {
readingCert = true;
cert = cert.concat(line.concat("\n"));
} else if (readingCert) {
cert = cert.concat(line.concat("\n"));
}
}
br.close();
return Either.right(cert);
} catch (Exception e) {
return Either.left(e);
}
}
};
}
開發者ID:fedora-infra,項目名稱:fedmsg-java,代碼行數:28,
示例3: mkPromise
點讚 3
import fj.data.Either; //導入依賴的package包/類
private static Promise mkPromise(final Strategy s) {
final Actor, Actor>, Promise>> q =
queueActor(s, new Effect1, Actor>, Promise>>() {
public void f(final P2, Actor>, Promise> p) {
final Promise snd = p._2();
final Queue> as = snd.waiting;
if (p._1().isLeft()) {
final A a = p._1().left().value()._1();
snd.v = some(a);
snd.l.countDown();
while (!as.isEmpty())
as.remove().act(a);
} else if (snd.v.isNone())
as.add(p._1().right().value());
else
p._1().right().value().act(snd.v.some());
}
});
return new Promise(s, q);
}
開發者ID:JayTheDonkey,項目名稱:rpg.java,代碼行數:21,
示例4: parse
點讚 3
import fj.data.Either; //導入依賴的package包/類
public static Either parse(String recordLine) {
String[] lineContent = recordLine.split(" \\| ");
Either gre = GenericRecord.genericRecord(lineContent);
if (gre.isLeft()) {
return Either.left(gre.left().value());
} else {
GenericRecord gr = gre.right().value();
switch (gr.getJobTitle()) {
case "Researcher": return Either.right(parseResearcher(gr));
case "Administrator": return Either.right(parseAdministrator(gr));
case "Lecturer": return Either.right(parseLecturer(gr));
default: return Either.left("Can't parse job title near: " + recordLine);
}
}
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:17,
示例5: interact
點讚 3
import fj.data.Either; //導入依賴的package包/類
public void interact() {
while (true) {
printMainMenu();
Either ei = Util.getPositiveInt_().run();
if (ei.isLeft()) {
System.out.println(ei.left().value());
} else {
switch (ei.right().value()) {
case 1 : System.out.println(database); break;
case 2 :
getMonthlyWage().>either(Util.print_(),
Util.print_());
break;
case 3 : recordAddRoutine(); break;
case 4 : recordDeleteRoutine(); break;
case 5 : System.exit(0); break;
default : System.out.println("Invalid choice."); continue;
}
}
}
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:24,
示例6: calculateWage
點讚 3
import fj.data.Either; //導入依賴的package包/類
@Override
public Either>, Money> calculateWage() {
F> f =
new F>() {
@Override
public Either f(Unit u) {
Either c =
Util.getPositiveDouble
("Enter number of overtime hours worked.");
if (c.isRight()) {
Money wage =
getSalary().getMonthly().add(c.right().value() * 10d);
return Either.right(wage);
} else {
return Either.left(c.left().value());
}
}
};
return Either.left(new IO<>(f));
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:25,
示例7: authenticateAndGetOrders
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static DB>> authenticateAndGetOrders(String email, String pass)
{
// with bind we take the result of one operation, and use it to return another operation
return UserDB.login(email, pass).bind(success -> {
if (!success)
{
Either> errorMessage = Either.left("auth failure");
// the DB.unit operation returns an immediate result with the passed value, without touching the connection
return DB.unit(errorMessage);
}
return selectOrdersByEmail(email).map(orders -> Either.right(orders));
});
}
開發者ID:novarto-oss,項目名稱:sane-dbc,代碼行數:16,
示例8: testIt
點讚 2
import fj.data.Either; //導入依賴的package包/類
@Test
public void testIt()
{
SyncDbInterpreter dbi = new SyncDbInterpreter(
() -> DriverManager.getConnection("jdbc:hsqldb:mem:bind_example", "sa", "")
);
dbi.submit(UserDB.CREATE_USER_TABLE);
dbi.submit(OrderDb.CREATE_TABLE);
dbi.submit(UserDB.insertUser("[email protected]", "abcd"));
dbi.submit(UserDB.insertUser("[email protected]m", "abcd"));
dbi.submit(
OrderDb.insertOrders(
arrayList(new CreateOrder("[email protected]", "Hi there"),
new CreateOrder("[email protected]", "Bye there")
))
);
Either> result = dbi.submit(OrderDb.authenticateAndGetOrders("[email protected]", "abcd"));
assertThat(result.isRight(), is(true));
List orders = result.right().value();
assertThat(orders.isSingle(), is(true));
Order johnOrder = orders.head();
assertThat(johnOrder.text, is("Hi there"));
Either> shouldFail = dbi.submit(OrderDb.authenticateAndGetOrders("haxx0r", "abcd"));
assertThat(shouldFail, is(Either.left("auth failure")));
}
開發者ID:novarto-oss,項目名稱:sane-dbc,代碼行數:35,
示例9: depth
點讚 2
import fj.data.Either; //導入依賴的package包/類
static public int depth(Tree t) {
Either.LeftProjection> left = t.toEither().left();
left.iterator().hasNext();
for (Empty e : t.toEither().left())
return 0;
for (Either ln : t.toEither().right()) {
for (Leaf leaf : ln.left())
return 1;
for (Node node : ln.right())
return 1 + max(depth(node.left), depth(node.right));
}
throw new RuntimeException("Inexhaustible pattern match on tree");
}
開發者ID:whyDK37,項目名稱:pinenut,代碼行數:14,
示例10: inTree
點讚 2
import fj.data.Either; //導入依賴的package包/類
static public boolean inTree(Tree t, int value) {
for (Empty e : t.toEither().left())
return false;
for (Either ln : t.toEither().right()) {
for (Leaf leaf : ln.left())
return value == leaf.n;
for (Node node : ln.right())
return inTree(node.left, value) | inTree(node.right, value);
}
return false;
}
開發者ID:whyDK37,項目名稱:pinenut,代碼行數:12,
示例11: occurrencesIn
點讚 2
import fj.data.Either; //導入依賴的package包/類
static public int occurrencesIn(Tree t, int value) {
for (Empty e : t.toEither().left())
return 0;
for (Either ln : t.toEither().right()) {
for (Leaf leaf : ln.left())
if (value == leaf.n) return 1;
for (Node node : ln.right())
return occurrencesIn(node.left, value) + occurrencesIn(node.right, value);
}
return 0;
}
開發者ID:whyDK37,項目名稱:pinenut,代碼行數:12,
示例12: either
點讚 2
import fj.data.Either; //導入依賴的package包/類
/**
* Turns the given Callable into either an exception or the value in the Callable.
*
* @param a The callable to convert to an Either value.
* @return Either the value in the given Callable, or the Exception with which the Callable fails.
*/
public static P1> either(final Callable
return new P1>() {
public Either _1() {
try {
return right(a.call());
} catch (Exception e) {
return left(e);
}
}
};
}
開發者ID:JayTheDonkey,項目名稱:rpg.java,代碼行數:18,
示例13: fromEither
點讚 2
import fj.data.Either; //導入依賴的package包/類
/**
* Turns a given Either value into the equivalent Callable.
*
* @param e Either an exception or a value to wrap in a Callable
* @return A Callable equivalent to the given Either value.
*/
public static Callable fromEither(final P1> e) {
return new Callable() {
public A call() throws Exception {
final Either e1 = e._1();
if (e1.isLeft())
throw e1.left().value();
else
return e1.right().value();
}
};
}
開發者ID:JayTheDonkey,項目名稱:rpg.java,代碼行數:18,
示例14: bind
點讚 2
import fj.data.Either; //導入依賴的package包/類
/**
* Binds the given function over this promise, with a final join.
* The bind function for the Promise monad.
*
* @param f The function to bind over this promise.
* @return The result of applying the given function to this promised value.
*/
public Promise bind(final F> f) {
final Promise r = mkPromise(s);
final Actor ab = actor(s, new Effect1() {
public void f(final B b) {
r.actor.act(P.p(Either., Actor>left(P.p(b)), r));
}
});
to(ab.promise().comap(f));
return r;
}
開發者ID:JayTheDonkey,項目名稱:rpg.java,代碼行數:18,
示例15: resume
點讚 2
import fj.data.Either; //導入依賴的package包/類
public Either>, A> resume() {
return left(sub.resume().either(p -> {
return p.map(ot -> {
// WARNING: In JDK 8, update 25 (current version) the following code is a
// workaround for an internal JDK compiler error, likely due to
// https:bugs.openjdk.java.net/browse/JDK-8062253.
F, Trampoline> f = o -> o.foldNormal(o1 -> cont.f(o1), t -> t._1().bind(cont));
F, Trampoline> g = c -> codense(c.sub, o -> c.cont.f(o).bind(cont));
return ot.fold(f, g);
});
}, o -> P.lazy(u -> cont.f(o))));
}
開發者ID:JayTheDonkey,項目名稱:rpg.java,代碼行數:13,
示例16: run
點讚 2
import fj.data.Either; //導入依賴的package包/類
/**
* Runs this computation all the way to the end, in constant stack.
*
* @return The end result of this computation.
*/
@SuppressWarnings("LoopStatementThatDoesntLoop")
public A run() {
Trampoline current = this;
while (true) {
final Either>, A> x = current.resume();
for (final P1> t : x.left()) {
current = t._1();
}
for (final A a : x.right()) {
return a;
}
}
}
開發者ID:JayTheDonkey,項目名稱:rpg.java,代碼行數:19,
示例17: eitherOrd
點讚 2
import fj.data.Either; //導入依賴的package包/類
/**
* An order instance for the {@link Either} type.
*
* @param oa Order across the left side of {@link Either}.
* @param ob Order across the right side of {@link Either}.
* @return An order instance for the {@link Either} type.
*/
public static Ord> eitherOrd(final Ord
return ord(e1 -> e2 -> e1.isLeft() ?
e2.isLeft() ?
oa.f.f(e1.left().value()).f(e2.left().value()) :
Ordering.LT :
e2.isLeft() ?
Ordering.GT :
ob.f.f(e1.right().value()).f(e2.right().value()));
}
開發者ID:JayTheDonkey,項目名稱:rpg.java,代碼行數:17,
示例18: calculateWage
點讚 2
import fj.data.Either; //導入依賴的package包/類
@Override
public Either>, Money> calculateWage() {
F> f =
new F>() {
@Override
public Either f(Unit u) {
Either c =
Util.getPositiveDouble
("Enter number of consultancy hours.");
if (c.isLeft()) {
return Either.left(c.left().value());
}
Either p =
Util.getPositiveDouble
("Enter yearly performance pay.");
if (p.isLeft()) {
return Either.left(p.left().value());
}
Money cp = new Money(c.right().value() * 20d);
Money pp = new Salary(new Money(p.right().value())).getMonthly();
return Either.right(getSalary().getMonthly().add(cp).add(pp));
}
};
return Either.left(new IO<>(f));
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:34,
示例19: getEmployee
點讚 2
import fj.data.Either; //導入依賴的package包/類
public Either getEmployee(Integer recordNumber) {
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i)._1() == recordNumber) {
return Either.right(employees.get(i)._2());
}
}
return Either.left(String.format("Record %d doesn't exist", recordNumber));
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:10,
示例20: getPositiveDouble_
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static IO> getPositiveDouble_() {
return new IO>
(new F>() {
@Override
public Either f(Unit u) {
return getPositiveDouble();
}
});
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:11,
示例21: getPositiveInt_
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static IO> getPositiveInt_() {
return new IO>
(new F>() {
@Override
public Either f(Unit u) {
return getPositiveDouble().right().map(Util.intValue_());
}
});
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:11,
示例22: getPositiveDoubleQ_
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static IO> getPositiveDoubleQ_(final String s) {
return new IO>
(new F>() {
@Override
public Either f(Unit u) {
return getPositiveDouble(s);
}
});
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:11,
示例23: getPositiveIntQ_
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static IO> getPositiveIntQ_(final String s) {
return new IO>
(new F>() {
@Override
public Either f(Unit u) {
return getPositiveDouble(s).right().map(Util.intValue_());
}
});
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:11,
示例24: getPositiveDouble
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static Either getPositiveDouble() {
BufferedReader r =
new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
String s = r.readLine();
if (s == null) {
continue;
} else {
Double d = Double.parseDouble(s);
if (d < 0) {
System.out.println("Please enter a number >= 0.");
continue;
} else {
return Either.right(d);
}
}
} catch (NumberFormatException e) {
System.out.println("Enter a valid number.");
continue;
} catch (IOException io) {
return Either.left("IOException has occured. Aborting input.");
}
}
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:29,
示例25: phoneNumber
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static Either
phoneNumber(final String number) {
if (isValid(number)) {
return Either.right(new PhoneNumber(number));
} else {
return Either.left
(String.format("%s is not a valid phone number.", number));
}
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:10,
示例26: main
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static void main(String[] args) {
/* Load records from a text file */
String recordFile = null;
switch (args.length) {
case 0: recordFile = "records.txt"; break;
case 1: recordFile = args[0]; break;
default: printUsage(); System.exit(2);
}
File f = new File(recordFile);
if (f.exists() && !f.isDirectory()) {
Either> employees = parseRecordFile(f);
if (employees.isLeft()) {
System.out.println(employees.left().value());
System.exit(2);
} else {
EmployeeDatabase dbase = new EmployeeDatabase();
/* Populate the database */
for (Employee emp : employees.right().value()) {
dbase.addEmployee(emp);
}
/* Make a sure to interface with our database. */
UserInterface ui = new UserInterface(dbase);
ui.interact();
}
} else {
System.out.println("Record file " + f.getName() + " doesn't exist.");
System.exit(3);
}
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:35,
示例27: recordDeleteRoutine
點讚 2
import fj.data.Either; //導入依賴的package包/類
private void recordDeleteRoutine() {
String message = "Which record to delete? 0 to cancel.";
Either record = Util.getPositiveIntQ_(message).run();
if (record.isLeft()) {
System.out.println(record.left().value());
} else {
if (record.right().value() != 0) {
boolean gotDeleted = database.deleteRecord(record.right().value());
if (!gotDeleted)
System.out.println("No such record.");
}
}
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:15,
示例28: recordAddRoutine
點讚 2
import fj.data.Either; //導入依賴的package包/類
private void recordAddRoutine() {
String format = "ID | Name | JobTitle | PhoneNumber | ";
format += "City : StreetAddress : Post-code";
String lecmsg = "Lecturer performance pay should be a last field.";
lecmsg += " No £ sign.";
String message = String.format("The record format is:\n%s\n%s\n%s",
format, lecmsg, "0 to exit.");
System.out.println(message);
while (true) {
try {
String record = reader.readLine();
if (record == null)
continue;
if ("0".equals(record)) {
break;
}
Either parseResult = RecordParser.parse(record);
if (parseResult.isLeft()) {
System.out.println("Failed to parse input. Check your format.");
System.out.println(parseResult.left().value());
}
else {
database.addEmployee(parseResult.right().value());
break;
}
}
catch (IOException e) {
System.out.println("IO Exception when read records. Aborting...");
System.exit(1);
}
}
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:35,
示例29: getMonthlyWage
點讚 2
import fj.data.Either; //導入依賴的package包/類
public Either getMonthlyWage() {
System.out.println(database);
System.out.println("Pick an employee by database record number.");
Either ei = Util.getPositiveInt_().run();
if (ei.isLeft()) {
return Either.left(ei.left().value());
} else {
Integer recordNumber = ei.right().value();
Either empr = database.getEmployee(recordNumber);
if (empr.isLeft()) {
return Either.left(empr.left().value());
} else {
final Employee emp = empr.right().value();
Either>, Money> wact = emp.calculateWage();
if (wact.isLeft()) {
Either r = wact.left().value().run();
if (r.isLeft()) {
return Either.left(r.left().value());
} else {
return Either.right(r.right().value());
}
} else {
return Either.right(wact.right().value());
}
}
}
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:31,
示例30: genericRecord
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static Either genericRecord(String[] line) {
try {
Integer id = Integer.parseInt(line[0]);
String name = line[1];
String jobTitle = line[2];
Either phoneNumber;
Address address;
if (id <= 0) {
return Either.left("ID value less than or 0.");
}
phoneNumber = PhoneNumber.phoneNumber(line[3]);
String[] ap = line[4].split(" : ");
address = new Address(ap[0], ap[1], ap[2]);
if (phoneNumber.isLeft()) {
return Either.left(phoneNumber.left().value());
} else {
return Either.right
(new GenericRecord(id, name, jobTitle,
phoneNumber.right().value(), address));
}
}
catch (ArrayIndexOutOfBoundsException b) {
System.out.println(b);
return Either.left("Unexpected address format.");
}
catch (NumberFormatException nf) {
System.out.println(nf);
return Either.left("Unexpected id format.");
}
}
開發者ID:Fuuzetsu,項目名稱:cm20215-cwk2,代碼行數:38,
示例31: logdocs
點讚 2
import fj.data.Either; //導入依賴的package包/類
public void logdocs() {
try {
logger.debug("**** Routes: ****");
logger.debug(Strings.mkString(RouteDocumentation.loggerShow, "\n").showS(routeDocumentations));
Path baseDirectory =
getBasedir();
Path sourceDir =
baseDirectory.resolve("src/main/java");
Path targetDirectory =
baseDirectory.resolve("target/generated-documentation");
Path targetPath =
Files.createDirectories(targetDirectory).resolve("doc.json");
Set dependencies =
Option.somes(
log.asList()
.bind(info -> iterableList(info.getBackendMessages()))
.map(info -> some(new DependencyDocumentation(info.getProtocol(), substringBefore(info.getAddress(), "?"))))
.append(routeDocumentations
.bind(RouteDocumentation::collectLabels)
.map(DependencyDocumentation::fromRouteLabel)
))
.foldLeft(Set::insert, Set.empty(DependencyDocumentation.ord));
logger.debug("**** Dependencies ****");
logger.debug(Strings.mkString(DependencyDocumentation.loggerShow, "\n").showS(dependencies.toList()));
List> docsAndDesc =
buildDoc(sourceDir);
List resourceDocs =
addRequestAndResponse(Either.rights(docsAndDesc), log);
logger.debug("**** Api docs ****");
logger.debug(Strings.mkString(ResourceDocumentation.loggerShow, "\n").showS(resourceDocs));
String desc = Either.lefts(docsAndDesc).headOption().orSome("N/A");
logger.debug("**** Plugin docs ****");
logger.debug(desc);
Model model = ModelBuilder.extractModel(routeDocumentations);
Map json = ModelBuilder.toJson(model);
PluginDocumentation doc = new PluginDocumentation(desc, dependencies.toList(), routeDocumentations, resourceDocs, json);
mapper.writeValue(targetDirectory.resolve("doc.json").toFile(), doc);
logger.info(String.format("Documenter plugin found %s resources, %s dependencies", resourceDocs.length(), dependencies.size()));
}
catch (Throwable e) {
logger.error("Fail!", e);
}
}
開發者ID:kantega,項目名稱:respiro,代碼行數:63,
示例32: Seek
點讚 2
import fj.data.Either; //導入依賴的package包/類
private Seek(Either from) {
NotNull.check(from);
this.from = from;
}
開發者ID:manuelp,項目名稱:jevsto,代碼行數:5,
示例33: seek
點讚 2
import fj.data.Either; //導入依賴的package包/類
public static Seek seek(Instant from) {
return new Seek(Either. left(from));
}
開發者ID:manuelp,項目名稱:jevsto,代碼行數:4,
示例34: getFrom
點讚 2
import fj.data.Either; //導入依賴的package包/類
public Either getFrom() {
return from;
}
開發者ID:manuelp,項目名稱:jevsto,代碼行數:4,
示例35: toEither
點讚 2
import fj.data.Either; //導入依賴的package包/類
public Either> toEither() {
return left(this);
}
開發者ID:whyDK37,項目名稱:pinenut,代碼行數:4,
示例36: Promise
點讚 2
import fj.data.Either; //導入依賴的package包/類
private Promise(final Strategy s, final Actor, Actor>, Promise>> qa) {
this.s = s;
actor = qa;
}
開發者ID:JayTheDonkey,項目名稱:rpg.java,代碼行數:5,
注:本文中的fj.data.Either類示例整理自Github/MSDocs等源碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。