Title: Java
1?????????????? ???????????????? ? Java
- ??????? ????????
- ekirpichov_at_gmail.com
- 2008
2????
- ?????????
- ????????? ??????????
- ????????? ? ????????? ??
- ???????
3?????????? Java
- ????
- ???????????? ????? ????? (???????? ?????????????)
- ?????????? ?????????? ??? ???????/????????/???????
?/ - ??????? ????????? ??? ????????? (?????????
???????) - ?????????? ???????? ? ?.?.
- ????????
- Kingdom of nouns
- new NumberAdder(new IntegerValue(2), new
IntegerValue(2)).performOperation() - Boilerplate is OK, ??? ?? Java
4??? ?? ?? ???????
- ????????
- ??????????? ???????
- ?????-???????, ? ????? ?????
- ????????? ?????? ? ???????? ????????
- ?????? SICP
- Abelson, Sussman Structure and Interpretation
of Computer Programs - ???? ?? ??????? ???????? ? ???????????
- ???????? ??????????? ?????
5????
- ?????????
- ????????? ??????????
- ????????? ? ????????? ??
- ???????
6????????? ??????????
- Generics / type inference
- ???????? ??????
- ???????? ????????
- Naming conventions
- ? ??????
7????????? ?????????? Generics
- MapltInteger, ListltStringgtgt namesById new
HashMapltInteger, ListltStringgtgt() - ListltPairltFoo,Bargtgt foosAndBars new
ArrayListltPairltFoo,Bargtgt() - MapltString, MapltString,Stringgtgt thingsByName
- new LinkedHashMapltString, MapltString,Stringgtgt().
?????? ?? ?? MapltInteger, ListltStringgtgt
namesById new HashMap() ListltPairltFoo,Bargtgt
foosAndBars new ArrayList() MapltString,
MapltString,Stringgtgt thingsByName new
LinkedHashMap() ?????? ??? ???????? raw-????, ?
?? ??, ??? ?????. ??? ????????????? ????? ?????
??????, ???? ??? ??????? ??? ???? ?? ???. ???
???? ?? ???????????? ???? ????????
8????????? ?????????? Generics
- ??? ???? ?? ???????????? ???? ????????
- public class CollectionFactory
- public static ltK,Vgt MapltK,Vgt
newUnorderedMap() - return new HashMapltK,Vgt()
-
- public static ltAgt ListltAgt newList()
- return new ArrayListltAgt()
-
-
-
- import static CollectionFactory.
- MapltInteger, ListltStringgtgt namesById
newUnorderedMap() - ListltPairltFoo,Bargtgt foosAndBars newList()
9????????? ?????????? ???????? ??????
- ???????
- compressImages(Image.JPEG, new String
foo.jpg, bar.jpg, baz.jpg) - ?????????
- private static final SetltStringgt FORBIDDEN_TAGS
newUnorderedSet() - static
- FORBIDDEN_TAGS.add(SCRIPT)
- FORBIDDEN_TAGS.add(INPUT)
- FORBIDDEN_TAGS.add(LINK)
-
- ???????
- MapltString,Stringgt params newLinkedMap()
- params.put(product_id, productId)
- params.put(action, get_price)
- params.put(historyLen, 5)
- request new HttpPOST(http//www.site.com/main.h
tml, params)
10????????? ?????????? ???????? ??????
- public class CollectionFactory
- public static ltTgt T array(T ts)
return ts - public static ltTgt ListltTgt listOf(T ts)
return Arrays.asList(ts) - public static ltTgt SetltTgt setOf(T ts)
return newUnorderedSet(ts) -
- /
- kvs is like key,value, key,value,
- /
- public static ltK,Vgt MapltK,Vgt mapOf(Object
kvs)
11????????? ?????????? ???????? ??????
- ???????
- compressImages(Image.JPEG, array(foo.jpg,
bar.jpg, baz.jpg)) - ?????????
- private static final SetltStringgt FORBIDDEN_TAGS
setOf(SCRIPT,LINK,INPUT) - ???????
- request new HttpPOST(http//www.site.com/main.h
tml, - mapOf(product_id, productId,
- action, get_price,
- historyLen, 5))
- ??????
- import static is a good thing.
- varargs are a good thing.
12????????? ?????????? ???????? ??????
- ???????????? ?????????? ??????????? DSL
- _at_Test
- public void testIsConnected()
- assertTrue (GraphAnalyzer.isConnected(graph(1
-gt2 2-gt3 3-gt1))) - assertFalse(GraphAnalyzer.isConnected(graph(1
-gt2 2-gt1 3-gt4))) -
- private static Graph graph(String representation)
- // ?????? ?????????? ? ????? ?? ??????
???????
13???????????? vs. ?????????? ???
- private static Graph graph(String
representation) - // ?????? ?????????? ? ????? ?? ??????
??????? -
- ??????????
- ?????????? ???
- ?????????? ?????????? ? ????????????
- ???????????? ???
- ?????????? ? ???????????? ??? ?????????????
- ?????????? ?????????? ? ???????????? ???????????
????
14????????? ?????????? ???????? ????????
- ?????? ????????????? ?? static factory method.
- Filter f new AndFilter(first, second)
- searchConditions.add(new FieldCondition(title,
- new StringCondition(Jjava",
- StringComparisonKind.REGEX,
- StringPosition.ANYWHERE))
- document.append(
- new Element(image, array(
- new Attribute(url, ), new
Attribute(title, )))) - ?? ??? ?? ? ?????, ?? ???? ?????? ?????!
15????????? ?????????? ???????? ????????
- ?????? ????????????? ?? static factory method.
- import static com.acme.util.Filters.
- Filter f and(first, second)
- import static com.acme.search.Conditions.
- searchConditions.add(onField(title,
regexAnywhere(Jjava)) - import static com.acme.document.Elements.
- document.append(image(url(), title())
- ????????????
- ??? ??????, ?????? ??????
- ??????, ? ??????? ?? ?????????????, ?????
???????? - regexAnywhere, regexWhole, exactMatchAnywhere,
exactMatchWhole - vs
- new StringCondition(Jjava",
16????????? ?????????? ?????????? ???????
- ????????? ??????? ????? ????? ????????
- ListltCustomergt customers map(orders, new
FunctionltOrder,Customergt() - public Customer apply(Order arg)
- return arg.getCustomer()
-
- )
- ??-??????, ?? ???? ?????????, ???? ??????
????????, ??? map/fold. - ListltCustomergt customers newList()
- for(Order ord orders)
- customers.add(ord.getCustomer())
17????????? ?????????? ?????????? ???????
- ?? ???? ?? ? ???????????? ????????? ?????, ??
????? ??? - private static final FunctionltOrder,Customergt
GET_CUSTOMER new FunctionltOrder,Customergt() - public Customer apply(Order arg)
- return arg.getCustomer()
-
-
- ListltCustomergt customers map(GET_CUSTOMER,
orders) - ??? ???????, ????
- ?????????? ??? ?????? ? ????????
- ? ???????? ????????? ????????????
- GET_CUSTOMER ????? ??????????? ??? ???.
- ?????????? ??? ????? ??????? ????????? ?
???????????? ???.
18????????? ?????????? ?????????? ???????
- ?????????? ??????????? ???????
- StringCondition regex new RegexStringCondition()
- CustomerProcessor taxes new ComputeTaxesCustomer
Processor() - WindowEventListener listener new
CaptureDoubleClicksWindowEventListener() - WTF?
- ??? ???????? ?? ?????.
- StringCondition regex new Regex()
- CustomerProcessor taxes new ComputeTaxes()
- WindowEventListener listener new
CaptureDoubleClicks() - ???????????? ???????? ????????? ????, ?? ???
????? ???????? package???, ? ???????? ??
????????? ?????? ????, ??? ??????? ? ????.
19????????? ?????????? ?????????? ???????
- class Type
- public boolean isParent(Type other)
-
- ??? ?????? this.isParent(other) ? ????????
other ??? ??? ???????? - other? - class DNASequence
- public int index(DNASequence other)
-
- class KungFu
- public boolean isBetter(KungFu yours)
-
- ?? ?? ???????.
20????????? ?????????? ?????????? ???????
- class Type
- public boolean isParentOf(Type other)
- ???
- public boolean isChildOf(Type other)
-
- class DNASequence
- public int indexOf(DNASequence other)
- ???
- public int indexIn(DNASequence other)
- (????????, ??? ? ???????
?????-???????????? ???????????) -
- class KungFu
- public boolean isBetterThan(KungFu yours)
21????????? ?????????? ?????????? ???????
- ????? ???????
- ???? relates ???????? ????????? ?????????, ??
x.relates(y) ?????? ???????? ?????????? ??? x
relates y. - ???? op ???????? ????????? ???????? ????????, ??
x.op(y) ?????? ???????? ???? ?????????? ??? x op
y, ???? ??? x, give me op(y) - Mary is parent John ????????????
- Mary is parent of John ??????????
- My kung fu is better yours ????????????
- My kung fu is better than yours - ??????????
- accel multiply dt ????????????
- accel multiplied by dt ??????????
- accel times dt ??????????
- genome index AGCTG ????????????
- genome, give me index of AGCTG - ??????????
22????????? ?????????? ?????????? ??????????
- ???????, ?????? ???, ?????????????.
- ???????? ??????? ?? ????
- MapltLong,Stringgt categories
- MapltLogEntry, ListltLogDescrgtgt fooLog
- MapltLogEntry, ListltLogDescrgtgt entries
- MultiMapltString,Stringgt siteGraph
- ?????????
- public class BarMapper
- private final MapltLong, Longgt bars new
HashMapltLong, Longgt() - public void setBars(MapltLong, Longgt bars)
- this.bars.putAll(bars)
-
- public MapltLong, Longgt getBars()
23????????? ?????????? ?????????? ??????????
- ???????, ?????? ???, ?????????????.
- MapltLong,Stringgt categoryId_Name
- MapltLogEntry, ListltLogDescrgtgt entry_bars
- MultiMapltString,Stringgt url_outLink
- public class BarMapper
- private final MapltLong, Longgt old_newBarId
newUnorderedMap() - public void setOld_NewBarId(MapltLong, Longgt
old_newBarId) - this.old_newBarId.putAll(old_newBarId)
-
- public SetltLonggt getOldBarIds()
- return old_newBarId.keySet()
-
24????
- ?????????
- ????????? ??????????
- ????????? ? ????????? ??
- ???????
25????????? ? ????????? ??
- ??????? ?????????? ?????????
- ??????? ? ?????????
- ??????? ???????????
26????????? ? ????????? ????????? ??????????
?????????
- ??? ???????????? filter(), map(), fold()?
- ListltFoogt foos map(GET_FOO, bars)
- ListltProductgt expensive filter(priceMoreThan(100
), products) - ????????? ??? ???????????? ?????????????
- ?? ????????? ??????????????
- ??? ????? ??? ????? command-line ?????????,
??????? ????? ???????? ?????? ? ???????, ? ?? ?
stdin/stdout ?????? ??????? ? ????????
???????????? ????????? - cat file.txt grep foo sed s/foo/bar/ sort
uniq c sort n - ??????????????? ?????
- vs
- grep foo file.txt gt tmp1.txt
- sed s/foo/bar/ tmp1.txt gt tmp2.txt
- sort tmp2.txt gt tmp3.txt
- uniq c tmp3.txt gt tmp4.txt
- sort n tmp4.txt gt tmp5.txt
- cat tmp5.txt
27????????? ? ????????? ????????? ??????????
?????????
- ?????? ?????? ?????????????? map(GET_FOO, _)
??? filter(priceMoreThan(100), _). - ???????
- ???????????? ????????
- ????? ??????????? ?? ??????????, ? ?????????
- sort Options -gt stdin -gt stdout
- sort n stdin -gt stdout ???????? ??? ???????
- vs
- sort Options -gt FileName -gt stdout
- sort n file.txt stdout
- public static ltK,Vgt FunctionltListltKgt,ListltVgtgt map
(FunctionltK,Vgt f) - public static ltKgt FunctionltListltKgt,ListltKgtgt
filter(FilterltKgt f) - vs
- public static ltK,Vgt ListltVgt map (FunctionltK,Vgt
f, ListltKgt ks) - public static ltKgt ListltKgt filter(FilterltKgt f,
ListltKgt ks)
28????????? ? ????????? ????????? ??????????
?????????
- public static ltK,Vgt FunctionltListltKgt,ListltVgtgt
map(FunctionltK,Vgt f) - public static ltK,Vgt FunctionltListltKgt,ListltKgtgt
filter(FilterltKgt f) - public abstract class FunctionltK,Vgt
- public abstract V apply(K argument)
-
- public final FunctionltK,Ugt then(FunctionltV,Ugt
second) -
- public class CollectionUtils
- public static ltV extends Comparablelt? super
Vgtgt - FunctionltIterableltKgt, Kgt
maximizeBy(FunctionltK,Vgt byWhat) - public static ltKgt
- FunctionltIterableltKgt, Vgt
over(FunctionltK,Booleangt filter,
FunctionltIterableltKgt, Vgt fun) -
-
29????????? ? ????????? ????????? ??????????
?????????
- ????? Order (Customer, Product, Time)
- FunctionltOrder,Customergt ORDER_CUSTOMER
- FunctionltOrder,Productgt ORDER_PRODUCT
- FunctionltProduct,Pricegt PRODUCT_PRICE
- FilterltProductgt categoryEquals(String pattern)
.. - // select month, agg(order) from orders group by
month - PartitionedltOrder,Month,Tgt byMonth(AggregateltOrder
, Tgt agg) .. - public PartitionedltOrder,Month,Customergt
mostGenerousCustomerByMonth() - FunctionltOrder, Pricegt ORDER_PRICE
ORDER_PRODUCT.then(ORDER_PRICE) - AggregateltOrder, Ordergt MOST_EXPENSIVE_ORDER
- over(ORDER_PRODUCT.then(categoryEquals("Ca
rs")), - maximizeBy(ORDER_PRICE))
- return byMonth(MOST_EXPENSIVE_ORDER.then(ORDER
_CUSTOMER)) -
-
30????????? ? ????????? ????????? ? ?????????
- ?????? ????? ??????????? ?????????? ???? ? ??????
- ? ????? ?? ??????
- ? ??????? ?? ?? ?????????
- ??? ????????????? ???????
- ????? ???????? ??? ???? ??? ? ??? ???????
31????????? ? ????????? ????????? ? ?????????
- ? ????? ??????? ?????
- ?????? ??? ????? ???????? ?? ???????? ?????????
- class BiRelationltL,Rgt
-
- ListltPairltL,Rgtgt allEntries()
- static BiRelationltL,Rgt rel(ListltPairltL,Rgtgt
pairs)
- BiRelationltR,Lgt flip()
- BiRelationltL,Rgt filterL(PredicateltLgt p)
- BiRelationltR,Lgt filterR(PredicateltRgt p)
return flip().filterL(p).flip()
- BiRelationltL,ListltRgtgt compressL()
- BiRelationltListltLgt,Rgt compressR()
return flip().compressL().flip()
32????????? ? ????????? ????????? ? ?????????
- ????? ? ??????
- private static BiRelationltNode, Nodegt
bfs(BiRelationltNode, Nodegt graph, Node root) - SetltNodegt roots singleton(root)
- SetltNodegt reached new
LinkedHashSetltNodegt() - reached.add(root)
- ListltPairltNode,Nodegtgt res newList()
- while(true)
- BiRelationltNode,Nodegt nextLayer
- graph.filterL(memberOf(roots)).f
ilterR(not(memberOf(reached))) - if(nextLayer.isEmpty())
- break
- res.addAll(nextLayer.allEntries())
- reached.addAll(roots
nextLayer.rightSet()) -
- return rel(res)
-
33????????? ? ????????? ????????? ? ?????????
- ?????????????? ??????
- ?????????? ??????? ?? ???????? ???????
- ??????? ????? ??????? ??????????? ????????? ??
O(N log N) ??? - ???????? ?? ?? ???? ????????? ?? O(N), ?????
???????????, ????? ?????? -
- public static FunctionltT, PairltT,Ugtgt
attachR(FunctionltT,Ugt fun) .. - public static FunctionltPairltT,Ugt, Tgt detachR()
.. - public static FunctionltPairltT,Ugt, Ugt detachL()
.. - public static ltU extends Comparablelt? super Ugtgt
- FunctionltListltTgt,ListltTgtgt sortBy(FunctionltT,Ugt
fun) - public static ltU extends Comparablelt? super Ugtgt
- FunctionltListltTgt,ListltTgtgt schwartzSortBy(Funct
ionltT,Ugt fun) -
- return map(attachR(fun)).then(sortBy(second())
).then(map(detachR()))
34????????? ? ????????? ????????? ???????????
- ??????? ??????? ?? map,filter,fold ???
???????????? ????? ????????????? ??????? - ???????? ???????? ?? GC
- ? ???????? ?????? ???, ?.?. ?????? ???????
- ??? ???????, ?? ????? ?????
- AggregateltCustomer, Ordergt everyonesOrdersIn2008
map(GET_ORDERS).then(concat()).then(filter(timeB
etween(year(2007), year(2008)))) - ??????????? ??????????? VLIW (Very Large
Instruction Word) - ???? ?????????? ????? ????????? ????????
- ??????????? ? ??????? ? ?????????????
- ????????? sin ? cos
- ??????? ??? ??
- public static ltK,Vgt AggregateltK,Vgt
concatMapOver(FunctionltK,Vgt fun, FilterltKgt
filter) - AggregateltCustomer, Ordergt everyonesOrdersIn2008
- concatMapOver(GET_ORDERS, timeBetween(year(200
7), year(2008))) - ??? ????? ??????????? ??????? ???????????, ???
????????????? ???????
35???????
- JDBC ?????? ? ?????? ??????
- ???????? ????????? ??????, ??????????,
????????????? ? ?.?.
36JDBC
- ListltTgt query(sql, RowMapperltTgt)
- ListltIntegergt foos db.query("select id from foo
where is_nice 1", - new RowMapperltIntegergt()
- public Integer mapRow(ResultSet rs, int
rownum) - return rs.getInt(1)
-
- )
- void query(sql, RowCallbackltTgt)
- MapltInteger,Stringgt id_name newUnorderedMap()
- db.query("select id, name from foo where
is_nice1", - new RowCallbackHandler()
- void handleRow(ResultSet rs, int rownum)
- id_name.put(rs.getInt(1),
rs.getString(2)) -
- )
37JDBC
- 80 ???? ?????
- ?????? ????? ????? ???????????
- ? ?????? ?????????? 95.3 ???? ?????
38JDBC
- public class RowMappers
- public static RowMapperltIntegergt getInt(final
int i) - return new RowMapperltIntegergt()
- public Integer mapRow(ResultSet rs,
int rownum) - return rs.getInt(i)
-
-
-
- public static RowMapperltStringgt
getString(final int i) - return new RowMapperlt String gt()
- public String mapRow(ResultSet rs,
int rownum) - return rs.getString(i)
-
-
-
-
-
39JDBC
- ??, ??? boilerplate ? ????? 80 ???? - ?????
- ?? boilerplate ????????????
- ??????????
- ?????????? ???
- ?????????? ??????????
- ???????????? ???
- ?????????? ??? ?????????????
- ?????????? ?????????? ??????????? ????
40JDBC
- ListltIntegergt foos db.query(
- "select id from foo where is_nice 1",
- new RowMapperltIntegergt()
- public Integer mapRow(ResultSet rs, int
rownum) - return rs.getInt(1)
-
- )
- vs
- ListltIntegergt foos db.query(
- "select id from foo where is_nice 1",
- RowMappers.getInt(1))
41JDBC
- ???????? ?????? SICP
- ???? ????????? ???????? ?????????? ????????
?????????? - ????????? ??? ????
- ???????? ?????????? ???? Java (???????????
??????????? ??????? ? ???????) - ???????? ?????????? Here comes the cool part
42JDBC
- RowMappers ???????? ??????????
- RowMapperltKgt RowMapperltVgt ? RowMapperltPairltK,Vgt
gt - RowMapperltKgt FunctionltK,Vgt/MapltK,Vgt ?
RowMapperltVgt - RowMapperltIntegergt ListltVgt ? RowMapperltVgt
- ??????? ?????? ???????????
- FunctionltInteger,Vgt indexList(ListltVgt)
- RowMapperltKgt K (??? ??????? ? ?????? null) ?
RowMapperltKgt - K.class RowMapper ? RowMapperltKgt (?????
????????????) - RowMapperltKgt simply(K) ??????????? ??????
43JDBC
- ???? ??????????
- public static ltK,Vgt RowMapperltVgt
fmap(FunctionltK,Vgt f, RowMapperltKgt keyMapper) - return new RowMapperltVgt()
- public V mapRow(ResultSet rs, int rownum)
- return f.apply(keyMapper.mapRow(rs,row
num)) -
-
-
- public static ltK,Vgt RowMapperltVgt
indexMapWith(MapltK,Vgt map, RowMapperltKgt
keyMapper) - return fmap(getFromMap(map), keyMapper)
-
44JDBC
- ??????
- MapltString,Stringgt synonyms ...
- ListltFoogt foos db.query(
- "select id, name, bar from foo where bar gt
10", - constructor(Foo.class,
- integerAt(1),
- nvl(indexMapWith(synonyms, stringAt(2)),
"unnamed"), - doubleAt(3)))
45JDBC
- RowCallbacks ?????????
- ?????? ???????????? ??? ??????????/???????? ?
??????? ? ????????? - addToCollection, removeFromCollection
- RowMapperltKgt CollectionltKgt ? RowCallback
- addToMap
- RowMapperltKgt RowMapperltVgt MapltK,Vgt ?
RowCallback - removeFromMap
- RowMapperltKgt MapltK,Vgt ? RowCallback
- ?????????? removeFromCollection(map.keySet())
- ??????????? ??? ??????, ????????
- addToReport(Report)
- printOnWriter(Writer)
46JDBC
- RowCallbacks ???????? ??????????
- chain ?????????? ???????? ?????????? RowCallback
- RowCallback ? RowCallback
- ifElse ????????? ? ??????????? ?? ????,
????????????? ?? ??? ????????? - RowMapperltBooleangt RowCallback RowCallback ?
RowCallback -
- ???? ?????????????, ????????? ????? ???????, ???
????????? ????? ? ???? ??????????? ?????????? - ????? ????????? ?????? ? ??????? ???????
47JDBC
- ???? ??????????
- public static RowCallback chain(RowCallback...
parts) - return new RowCallback()
- public void processRow(ResultSet rs, int
rownum) - for(RowCallback p parts)
- p.processRow(rs,rownum)
-
-
-
- public static RowCallback ifElse(
- RowMapperltBooleangt condition, RowCallback
then, RowCallback otherwise) -
- return new RowCallback()
- public void processRow(ResultSet rs, int
rownum) - if(Boolean.TRUE condition.mapRow(rs
,rownum)) - then.processRow(rs,rownum)
- else
48JDBC
- ??????
- MapltInteger,Doublegt rich newUnorderedMap(),
poor newUnorderedMap() - db.query(
- "select id, salary, (case when salary gt 100
then 1 else 0 end) " - "from employees",
- ifElse(booleanAt(3),
- addToMap(rich, integerAt(1),
doubleAt(2)), - addToMap(poor, integerAt(1),
doubleAt(2))))
49?????? ????????
- ?????????
- ????????? ??????
- ??????????
- ?????????????, ???????????
- ?????????? ? ??????????? ????????
- ????????? ??? ??? ??????
- ????????? ?? ?????? ??????
- ????????? ???????????? ??????
50?????? ????????
- ??????
- ? ???????? ???? ?????????
- ???????? ????? ??????????
- 2 ?????? (OK?????????)/(Failure)
- ????? ?????? ??? Outcome
- ? ????? ??????????? ??? ? ??? ?? ??????
51?????? ????????
- ? ??? ?? ??????
- 2 ?????? (OK?????????)/(Failure)
- ???? ?? ?? ????????? ?? ?????, ??? ?????? ?
?????? OK, ? ??? ?????? ? ?????? Failure - ??????? ??? ?????
- ????????? ???????? ???????????
- ??? ?????? ? ???????????, ???? OK
- ??? ?????? ? ???????, ???? Failure
- ??? Continuation-passing style
52?????? ????????
- ?????????? API
- interface ActionltTgt
- void run(ContltTgt cont)
-
- interface ContltTgt
- void ok(_at_NotNull T result)
- void fail(Failure failure)
-
- ??????? ???? ?????????? ???????
- ?????? action() action.run(new ContltFoogt() )
- ?????? foo() bar() ???? ????? ???
- ?? ??? ????? ??????????????
- ????? ??????? ???? ?????????? ????????
53?????? ????????
- ??????? API
- public class Actions
- public static ltTgt _at_NotNull T run(ActionltTgt
action) throws ActionFailed - final T result new T1
- final Failure failure new Failure1
- action.run(new ContltTgt()
- void ok(_at_NotNull t res) result0
res - void fail(Failure f) failure0
f - )
- if(result0 ! null)
- return result0
- else
- throw new ActionFailed(failure0)
-
-
54?????? ????????
- ?????????
- ActionltAgt justReturn(A a)
- ActionltAgt alwaysFail(Failure f)
- ActionltBooleangt trueAfter(long millis)
55?????? ????????
- ??????????? ????????? ??????
- ActionltAgt withDefault(ActionltAgt a, A defaultA)
- ActionltAgt orElse(ActionltAgt tryThis, ActionltAgt
ifFailedUseThis) - ????? ????
- ActionltAgt withRescue(ActionltAgt a,
FunctionltFailure, ActionltAgtgt rescue) - ActionltAgt withTimeout(ActionltAgt a, long timeout,
Failure onTimeout) - ActionltAgt withFinally(ActionltAgt a, Runnable
finalizer) - ActionltAgt withRollback(ActionltAgt a,
CallableltFailuregt rollback) - ActionltAgt withRetry(ActionltAgt a, int nAttempts,
long waitIntervalMillis) -
56?????? ????????
- ??????????? ????????????? ? ???????????
- ActionltAgt asynchronously(ActionltAgt a) // Cont
?????? ?? ?????? ???? - ActionltAgt earliestOf(ActionltAgt rivals)
- ActionltAgt awaitAllOf(ActionltAgt actions)
- ActionltAgt after(long delay, ActionltAgt a)
- ActionltBgt parFold(Function2ltA,B,Bgt reducer, B
seed, ActionltAgt actions) -
57?????? ????????
- ??????????? ??????????????????
- ActionltBgt then(ActionltAgt first,
FunctionltA,ActionltBgtgt second) - ???????? ???????? ????????? ??????. ??? ??????
Cont. - return justReturn
- (gtgt) then
58?????? ????????
- ???? ??????????
- public static ltA,Bgt ActionltBgt then(ActionltAgt
first, FunctionltA,ActionltBgtgt second) - return new ActionltBgt()
- public void run(final ContltBgt cont)
- first.run(new ContltAgt()
- public void ok(A a)
- second.apply(a).run(cont)
-
- public void fail(Failure f)
- cont.fail(f)
-
- )
-
-
-
59?????? ????????
- ???? ??????????
- public static ltAgt ActionltAgt earliestOf(ActionltAgt..
. rivals) - return new ActionltAgt()
- public void run(final ContltAgt cont)
- final CountDownLatch latch new
CountDownLatch(1) -
- class Hook implements ContltAgt
- private boolean arrived false
- private Failure someFailure
null - private int failures 0
- public void ok(A res)
if(arrived()) cont.ok(res) done() - public void fail(Failure f)
if(failed()) cont.fail(f) done() - private void done()
latch.countDown() - private synchronized boolean
arrived() - return arrived ? false
(arrivedtrue)
60?????? ????????
- ??? ???? ????????????
- ?????????? ??????? ????????? ? ?????????? ??????,
????????????? ?? ?????? ?? 3 ???? ? ????? ??
?????? ??????? ?? ????? 25 ??????. - ActionltPagegt simplyFetch(String url)
- return ...
- try
- cont.ok(pageFetcher.fetch(url))
- catch(Exception e)
- cont.fail(new Failure(e))
-
-
-
- ActionltPagegt asyncFetchMirrorsWithRetry(String...
urls) - ListltActionltPagegtgt retriers newList()
- for(String url urls)
- retriers.add(retry(
- withTimeout(simplyFetch(url), 25000,
new PageTimedOut(url)), - 5000, 3))
61?????
- Java ????? ???? ??????????
- ?? Java ????? ?????? ? ?????????????? ?????
- ???? ????? ?? ????? ????????? ??????? ??
????????? - ??????? ???? ?????????? ???????
- ?? ?????????????? API ??? ?????????? ????? are a
good thing - ??? ????????? ????????????? ???? ?? ?????
- ??????? ???? ?????????? ????????
- ??????
- ??????????
- ??????? ?????????? (80 ????? ? ?????????
?????) - ???? ?????????? ???? ??????, ? ??????? ?????
??????????? - ???? ??????? ??? ??????? ???? ???
62???????!