Example usage for java.util.concurrent.atomic AtomicBoolean getAndSet

List of usage examples for java.util.concurrent.atomic AtomicBoolean getAndSet

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicBoolean getAndSet.

Prototype

public final boolean getAndSet(boolean newValue) 

Source Link

Document

Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle#getAndSet .

Usage

From source file:com.example.android.supportv7.util.DiffUtilActivity.java

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout ll = new LinearLayout(this);
    RecyclerView rv = new RecyclerView(this);
    Button shuffle = new Button(this);
    shuffle.setText("Shuffle");
    ll.addView(shuffle);//w ww .  j a v a 2 s.  c o m
    ll.addView(rv);
    rv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    rv.setLayoutManager(new LinearLayoutManager(this));
    List<String> cheeseList = createRandomCheeseList(Collections.<String>emptyList(), 50);
    final SimpleStringAdapter adapter = new SimpleStringAdapter(this,
            cheeseList.toArray(new String[cheeseList.size()]));
    rv.setAdapter(adapter);
    final AtomicBoolean refreshingList = new AtomicBoolean(false);
    shuffle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (refreshingList.getAndSet(true)) {
                // already refreshing, do not allow modifications
                return;
            }
            //noinspection unchecked
            new AsyncTask<List<String>, Void, Pair<List<String>, DiffUtil.DiffResult>>() {

                @Override
                protected Pair<List<String>, DiffUtil.DiffResult> doInBackground(List<String>... lists) {
                    List<String> oldList = lists[0];
                    List<String> newList = createRandomCheeseList(oldList, 5);
                    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyCallback(oldList, newList));
                    //noinspection unchecked
                    return new Pair(newList, diffResult);
                }

                @Override
                protected void onPostExecute(Pair<List<String>, DiffUtil.DiffResult> resultPair) {
                    refreshingList.set(false);
                    adapter.setValues(resultPair.first);
                    resultPair.second.dispatchUpdatesTo(adapter);
                    Toast.makeText(DiffUtilActivity.this, "new list size " + resultPair.first.size(),
                            Toast.LENGTH_SHORT).show();
                }
            }.execute(adapter.getValues());

        }
    });
    setContentView(ll);
}

From source file:org.polymap.p4.process.ModuleProcessPanel.java

protected void fillOutputFields() {
    outputSection.getBody()/*  www.  j a v  a  2 s.c o  m*/
            .setLayout(ColumnLayoutFactory.defaults().columns(1, 1).margins(0, 8).spacing(10).create());

    AtomicBoolean isFirst = new AtomicBoolean(true);
    for (FieldInfo fieldInfo : moduleInfo.get().outputFields()) {
        if (fieldInfo.description.get().isPresent()) {
            // separator
            if (!isFirst.getAndSet(false)) {
                Label sep = new Label(outputSection.getBody(), SWT.SEPARATOR | SWT.HORIZONTAL);
                UIUtils.setVariant(sep, DefaultToolkit.CSS_SECTION_SEPARATOR); // XXX
            }
            // field
            FieldViewer fieldViewer = new FieldViewer(
                    new FieldViewerSite().moduleInfo.put(moduleInfo.get()).module.put(module).fieldInfo
                            .put(fieldInfo).layer.put(layer.get()));
            fieldViewer.createContents(outputSection.getBody())
                    .setLayoutData(ColumnDataFactory.defaults().widthHint(300).create());
            inputFields.add(fieldViewer);
        }
    }
}

From source file:info.archinnov.achilles.it.TestJSONCall.java

@Test
public void should_insert_json_if_not_exists() throws Exception {
    //Given/*from   ww w .  j av a2s .  com*/
    final long id = RandomUtils.nextLong(0L, Long.MAX_VALUE);

    //When
    String json = "{\"id\": " + id + ", \"clust\": 1, \"value\": \"val\", " + "\"liststring\": [\"one\"], "
            + "\"setstring\": [\"two\"], " + "\"mapstring\": {\"3\": \"three\"}" + "}";

    AtomicBoolean success = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    manager.crud().insertJSON(json).ifNotExists().withLwtResultListener(new LWTResultListener() {

        @Override
        public void onSuccess() {
            success.getAndSet(true);
            latch.countDown();
        }

        @Override
        public void onError(LWTResult lwtResult) {
            latch.countDown();
        }
    }).execute();

    //Then
    latch.await();
    assertThat(success.get()).isTrue();
    final Row row = session.execute(
            "SELECT * FROM achilles_embedded.entity_for_json_function_call WHERE id = " + id + "AND clust = 1")
            .one();
    assertThat(row).isNotNull();
    assertThat(row.getString("value")).isEqualTo("val");
    assertThat(row.getList("liststring", String.class)).containsExactly("one");
    assertThat(row.getSet("setstring", String.class)).containsExactly("two");
    assertThat(row.getMap("mapstring", Integer.class, String.class)).hasSize(1).containsEntry(3, "three");
}

From source file:org.polymap.p4.process.ProcessModulePanel.java

protected void fillOutputFields() {
    outputSection.getBody()/* w  w  w. j a v a 2 s . c om*/
            .setLayout(ColumnLayoutFactory.defaults().columns(1, 1).margins(0, 8).spacing(10).create());

    AtomicBoolean isFirst = new AtomicBoolean(true);
    for (FieldInfo fieldInfo : moduleInfo.outputFields()) {
        if (fieldInfo.description.get().isPresent()) {
            // separator
            if (!isFirst.getAndSet(false)) {
                Label sep = new Label(outputSection.getBody(), SWT.SEPARATOR | SWT.HORIZONTAL);
                UIUtils.setVariant(sep, DefaultToolkit.CSS_SECTION_SEPARATOR); // XXX
            }
            // field
            FieldViewer fieldViewer = new FieldViewer(
                    new FieldViewerSite().moduleInfo.put(moduleInfo).module.put(module).fieldInfo
                            .put(fieldInfo).layer.put(layer));
            fieldViewer.createContents(outputSection.getBody())
                    .setLayoutData(ColumnDataFactory.defaults().widthHint(300).create());
            inputFields.add(fieldViewer);
        }
    }
}

From source file:org.polymap.p4.process.ModuleProcessPanel.java

protected IPanelSection createInputSection() {
    IPanelSection section = tk().createPanelSection(parent, "Input", SWT.BORDER);
    section.getBody()/*w  w  w. j  av a  2 s  .c o m*/
            .setLayout(ColumnLayoutFactory.defaults().columns(1, 1).margins(0, 8).spacing(10).create());

    Label label = tk().createLabel(section.getBody(),
            moduleInfo.get().description.get().orElse("No description."), SWT.WRAP);
    label.setLayoutData(ColumnDataFactory.defaults().widthHint(300).create());
    label.setEnabled(false);

    AtomicBoolean isFirst = new AtomicBoolean(true);
    for (FieldInfo fieldInfo : moduleInfo.get().inputFields()) {
        // skip
        if (IJGTProgressMonitor.class.isAssignableFrom(fieldInfo.type.get())
                || !fieldInfo.description.get().isPresent()) {
            continue;
        }
        // separator
        if (!isFirst.getAndSet(false)) {
            Label sep = new Label(section.getBody(), SWT.SEPARATOR | SWT.HORIZONTAL);
            UIUtils.setVariant(sep, DefaultToolkit.CSS_SECTION_SEPARATOR); // XXX
        }
        // field
        FieldViewer fieldViewer = new FieldViewer(
                new FieldViewerSite().moduleInfo.put(moduleInfo.get()).module.put(module).fieldInfo
                        .put(fieldInfo).layer.put(layer.get()));
        fieldViewer.createContents(section.getBody())
                .setLayoutData(ColumnDataFactory.defaults().widthHint(300).create());
        inputFields.add(fieldViewer);
    }

    return section;
}

From source file:org.polymap.p4.process.ProcessModulePanel.java

protected IPanelSection createInputSection() {
    IPanelSection section = tk().createPanelSection(parent, "Input", SWT.BORDER);
    section.getBody()/*www . ja va2 s .c o  m*/
            .setLayout(ColumnLayoutFactory.defaults().columns(1, 1).margins(0, 8).spacing(10).create());

    Label label = tk().createLabel(section.getBody(), moduleInfo.description.get().orElse("No description."),
            SWT.WRAP);
    label.setLayoutData(ColumnDataFactory.defaults().widthHint(300).create());
    label.setEnabled(false);

    AtomicBoolean isFirst = new AtomicBoolean(true);
    for (FieldInfo fieldInfo : moduleInfo.inputFields()) {
        // skip
        if (IJGTProgressMonitor.class.isAssignableFrom(fieldInfo.type.get())
                || !fieldInfo.description.get().isPresent()) {
            continue;
        }
        // separator
        if (!isFirst.getAndSet(false)) {
            Label sep = new Label(section.getBody(), SWT.SEPARATOR | SWT.HORIZONTAL);
            UIUtils.setVariant(sep, DefaultToolkit.CSS_SECTION_SEPARATOR); // XXX
        }
        // field
        FieldViewer fieldViewer = new FieldViewer(
                new FieldViewerSite().moduleInfo.put(moduleInfo).module.put(module).fieldInfo
                        .put(fieldInfo).layer.put(layer));
        fieldViewer.createContents(section.getBody())
                .setLayoutData(ColumnDataFactory.defaults().widthHint(300).create());
        inputFields.add(fieldViewer);
    }

    return section;
}

From source file:io.cettia.DefaultServer.java

private DefaultServerSocket createSocket(ServerTransport transport) {
    Map<String, String> options = new LinkedHashMap<>();
    options.put("heartbeat", Integer.toString(heartbeat));
    options.put("_heartbeat", Integer.toString(_heartbeat));
    final DefaultServerSocket socket = new DefaultServerSocket(options);
    // socket.uri should be available on socket event #4
    socket.transport = transport;/*from   w  w  w . ja v a 2s  .c o  m*/
    // A temporal implementation of 'once'
    final AtomicBoolean done = new AtomicBoolean();
    socket.onopen(new VoidAction() {
        @Override
        public void on() {
            if (!done.getAndSet(true)) {
                sockets.put(socket.id, socket);
                socket.ondelete(new VoidAction() {
                    @Override
                    public void on() {
                        sockets.remove(socket.id);
                    }
                });
            }
        }
    });
    return socket;
}

From source file:info.archinnov.achilles.it.TestCRUDSimpleEntity.java

@Test
public void should_delete_with_equal_condition() throws Exception {
    //Given/*w  w  w .  j a v a2 s .  co  m*/
    final long id = RandomUtils.nextLong(0L, Long.MAX_VALUE);
    final Date date = buildDateKey();
    scriptExecutor.executeScriptTemplate("SimpleEntity/insert_single_row.cql",
            ImmutableMap.of("id", id, "table", "simple"));

    final AtomicBoolean success = new AtomicBoolean(false);
    final LWTResultListener lwtResultListener = new LWTResultListener() {

        @Override
        public void onSuccess() {
            success.getAndSet(true);
        }

        @Override
        public void onError(LWTResult lwtResult) {

        }
    };
    //When
    manager.dsl().delete().allColumns_FromBaseTable().where().id_Eq(id).date_Eq(date)
            .ifSimpleSet_Eq(Sets.newHashSet(1.0, 2.0)).withLwtResultListener(lwtResultListener).execute();

    //Then
    final Row row = session.execute("SELECT * FROM simple WHERE id = " + id).one();
    assertThat(row).isNull();
    assertThat(success.get()).isTrue();
}

From source file:info.archinnov.achilles.it.TestCRUDSimpleEntity.java

@Test
public void should_delete_with_inequal_condition() throws Exception {
    //Given//from   ww w . ja  va 2 s.  c o m
    final long id = RandomUtils.nextLong(0L, Long.MAX_VALUE);
    final Date date = buildDateKey();
    scriptExecutor.executeScriptTemplate("SimpleEntity/insert_single_row.cql",
            ImmutableMap.of("id", id, "table", "simple"));

    final AtomicBoolean success = new AtomicBoolean(false);
    final LWTResultListener lwtResultListener = new LWTResultListener() {

        @Override
        public void onSuccess() {
            success.getAndSet(true);
        }

        @Override
        public void onError(LWTResult lwtResult) {

        }
    };
    //When
    manager.dsl().delete().allColumns_FromBaseTable().where().id_Eq(id).date_Eq(date).ifValue_Lt("_")
            .withLwtResultListener(lwtResultListener).execute();

    //Then
    final Row row = session.execute("SELECT * FROM simple WHERE id = " + id).one();
    assertThat(row).isNull();
    assertThat(success.get()).isTrue();
}

From source file:info.archinnov.achilles.it.TestCRUDSimpleEntity.java

@Test
public void should_delete_with_not_equal_condition() throws Exception {
    //Given//  ww  w  .ja  v  a2 s  .c  o  m
    final long id = RandomUtils.nextLong(0L, Long.MAX_VALUE);
    final Date date = buildDateKey();
    scriptExecutor.executeScriptTemplate("SimpleEntity/insert_single_row.cql",
            ImmutableMap.of("id", id, "table", "simple"));

    final AtomicBoolean success = new AtomicBoolean(false);
    final LWTResultListener lwtResultListener = new LWTResultListener() {

        @Override
        public void onSuccess() {
            success.getAndSet(true);
        }

        @Override
        public void onError(LWTResult lwtResult) {

        }
    };
    //When
    manager.dsl().delete().allColumns_FromBaseTable().where().id_Eq(id).date_Eq(date)
            .ifConsistencyList_NotEq(Arrays.asList(ALL)).withLwtResultListener(lwtResultListener).execute();

    //Then
    final Row row = session.execute("SELECT * FROM simple WHERE id = " + id).one();
    assertThat(row).isNull();
    assertThat(success.get()).isTrue();
}