public Either<B, A> swap() { A vLeft = null; B vRight = null; if (left.isDefined()) { vLeft = left.get(); } if (right.isDefined()) { vRight = right.get(); } return new Either<B, A>(vRight, vLeft); }
public <X> Either<A, B> fold(Action<A> fa, Action<B> fb) { if (isLeft()) { fa.apply(left.get()); return new Either<A, B>(left.get(), null); } else if (isRight()) { fb.apply(right.get()); return new Either<A, B>(null, right.get()); } else { return new Either<A, B>(null, null); } }
@Test public void testLeft() { Left left = new Left(null, expr("0123456789", 4)); assertEquals("0123", left.eval(null)); left = new Left(null, expr("0123456789", 11)); assertEquals("0123456789", left.eval(null)); left = new Left(null, expr("0123456789", 0)); assertEquals("", left.eval(null)); left = new Left(null, expr(1234, 2)); assertEquals("12", left.eval(null)); }
@Test public void toString_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); String actual = target.toString(); String expected = "Left(foo)"; assertThat(actual, is(equalTo(expected))); }
@Test public void right_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); Option<Integer> actual = target.right(); assertThat(actual.isDefined(), is(false)); assertThat(actual.getOrNull(), is(nullValue())); }
@Test public void left_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); Option<String> actual = target.left(); assertThat(actual.isDefined(), is(true)); assertThat(actual.getOrNull(), is(equalTo("foo"))); }
@Test public void isRight_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); boolean actual = target.isRight(); boolean expected = false; assertThat(actual, is(equalTo(expected))); }
@Test public void swap_A$() throws Exception { Either<String, Integer> target = Left.apply("foo"); Either<Integer, String> swapped = target.swap(); Option<String> right = swapped.right(); assertThat(right.isDefined(), is(true)); assertThat(right.getOrNull(), is(equalTo("foo"))); }
public <X> Option<X> fold(Function<A, X> fa, Function<B, X> fb) { if (isLeft()) { return Option.maybe(fa.apply(left.get())); } else if (isRight()) { return Option.maybe(fb.apply(right.get())); } else { return (Option<X>) Option.none(); } }
@Test public void mergeToLeft_A$Function1() throws Exception { String value = "xxx"; Either<String, Integer> target = Left.apply(value); Function1<Integer, String> rightToLeft = new F1<Integer, String>() { public String apply(Integer v) { return v.toString(); } }; String actual = target.mergeToLeft(rightToLeft); String expected = "xxx"; assertThat(actual, is(equalTo(expected))); }
@Test public void mergeToRight_A$Function1() throws Exception { String value = "xxx"; Either<String, Integer> target = Left.apply(value); Function1<String, Integer> leftToRight = new F1<String, Integer>() { public Integer apply(String v) throws Exception { return v.length(); } }; Integer actual = target.mergeToRight(leftToRight); Integer expected = 3; assertThat(actual, is(equalTo(expected))); }
/** * This method is called from within the constructor to initialize the form. WARNING: Do NOT * modify this code. The content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); picturePaint1 = new PicturePaint(); Advance = new javax.swing.JButton(); Right = new javax.swing.JButton(); Left = new javax.swing.JButton(); BackGround = new javax.swing.JPanel(); locationName = new javax.swing.JLabel(); Direction = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 462, Short.MAX_VALUE)); jPanel1Layout.setVerticalGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE)); Advance.setText("Forward"); Advance.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AdvanceActionPerformed(evt); } }); picturePaint1.add(Advance); Advance.setBounds(290, 500, 90, 50); Right.setText("Right"); Right.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { RightActionPerformed(evt); } }); picturePaint1.add(Right); Right.setBounds(570, 500, 90, 50); Left.setText("Left"); Left.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { LeftActionPerformed(evt); } }); picturePaint1.add(Left); Left.setBounds(0, 500, 80, 50); locationName.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N locationName.setForeground(new java.awt.Color(255, 255, 255)); locationName.setText("jLabel1"); Direction.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N Direction.setForeground(new java.awt.Color(255, 255, 255)); Direction.setText("jLabel1"); javax.swing.GroupLayout BackGroundLayout = new javax.swing.GroupLayout(BackGround); BackGround.setLayout(BackGroundLayout); BackGroundLayout.setHorizontalGroup( BackGroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( BackGroundLayout.createSequentialGroup() .addContainerGap(20, Short.MAX_VALUE) .addGroup( BackGroundLayout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, BackGroundLayout.createSequentialGroup() .addComponent( locationName, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, BackGroundLayout.createSequentialGroup() .addComponent(Direction) .addGap(28, 28, 28))))); BackGroundLayout.setVerticalGroup( BackGroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( BackGroundLayout.createSequentialGroup() .addComponent( locationName, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Direction) .addGap(0, 19, Short.MAX_VALUE))); picturePaint1.add(BackGround); BackGround.setBounds(240, 10, 190, 80); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout .createSequentialGroup() .addContainerGap() .addComponent( jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(189, Short.MAX_VALUE)) .addComponent( picturePaint1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup() .addComponent( picturePaint1, javax.swing.GroupLayout.PREFERRED_SIZE, 556, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); pack(); } // </editor-fold>//GEN-END:initComponents
@Test public void __A$Option() throws Exception { Either<String, Integer> target = Left.apply(Option.apply("foo")); assertThat(target, notNullValue()); }
public boolean isLeft() { return left.isDefined(); }