如何检查是否不使用Javascript按位运算符?

时间:2017-06-30 13:09:27

标签: javascript bitwise-operators

我有一个函数,通过检查用户的标志来检查用户是否是高级用户:

package com.example.waheed.library;

import java.util.Calendar;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SearchViewActivity extends Activity implements 
SearchView.OnQueryTextListener,
    SearchView.OnCloseListener {

private ListView mListView;
private SearchView searchView;
private LibraryDbAdapter mDbHelper;

private TextView inspectionDate;
private TextView bookName;
private TextView bookAuthor;
private TextView storeName;
private TextView storeAddress;
private TextView storeLat;
private TextView storeLong;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    searchView = (SearchView) findViewById(R.id.search);
    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextListener(this);
    searchView.setOnCloseListener(this);

    mListView = (ListView) findViewById(R.id.list);
    inspectionDate = (TextView) findViewById(R.id.inspectionDate);
    displayDate();

    mDbHelper = new LibraryDbAdapter(this);
    mDbHelper.open();

    //Clean all Customers
    mDbHelper.deleteAllLibrary();
    //Add some Customer data as a sample
    mDbHelper.createLibrary("How to Win Friends and Influence People", "Dale Carnegie", "Book Centre", " Haider Rd, Rawalpindi 46000", "33.596651", "73.049094");
    mDbHelper.createLibrary("The 7 Habits of Highly Effective People", "Stephen Covey", "Idris Book Bank", "Bank Rd, Rawalpindi 46000", "33.595789", "73.052405");
    mDbHelper.createLibrary("Think and Grow Rich", "Napoleon Hill", "Students Book Company", "Bank Rd, Rawalpindi 44000", "33.596524", "73.051051");
    mDbHelper.createLibrary("Psycho-Cybernetics", "Maxwell Maltz", "Old Book Bank", "Green Super Market, Kashmir Rd, Saddar, Rawalpindi, Punjab", "33.595810", "73.051530");
    mDbHelper.createLibrary("How to stop worrying and start living", "Dale Carnegie", "Saeed Book Bank", "F-, Street 7, Islamabad 44000", "33.722446", "73.058763");
    mDbHelper.createLibrary("A New Earth", " Eckhart Tolle", "Variety Books", "41-BC, Bank Rd, Saddar, Rawalpindi 46000", "33.597497", "73.049917");
    mDbHelper.createLibrary("Blink: The Power of Thinking Without Thinking", "Malcolm Gladwell", "Rehan Book Shop", "Islamabad", "33.689734", "73.031652");
    mDbHelper.createLibrary("First Things First", "Stephen Covey", "Idris Book Bank", "Bank Rd, Rawalpindi 46000", "33.595789", "73.052405");
    mDbHelper.createLibrary("Emotional Intelligence", "Daniel Goleman", "Book Centre", " Haider Rd, Rawalpindi 46000", "33.596651", "73.049094");
    mDbHelper.createLibrary("Anatomy of the Spirit", "Caroline Myss", "Students Book Company", "Bank Rd, Rawalpindi 44000", "33.596524", "73.051051");

}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mDbHelper  != null) {
        mDbHelper.close();
    }
}

public boolean onQueryTextChange(String newText) {
    showResults(newText + "*");
    return false;
}

public boolean onQueryTextSubmit(String query) {
    showResults(query + "*");
    return false;
}

public boolean onClose() {
    showResults("");
    return false;
}

private void showResults(String query) {

    Cursor cursor = mDbHelper.searchBook((query != null ? query.toString() : "@@@@"));

    if (cursor == null) {
        //
    } else {
        // Specify the columns we want to display in the result
        String[] from = new String[]{
                LibraryDbAdapter.KEY_BOOKNAME,
                LibraryDbAdapter.KEY_BOOKAUTHOR,
                LibraryDbAdapter.KEY_STORENAME,
                LibraryDbAdapter.KEY_STOREADDRESS,
                LibraryDbAdapter.KEY_STORELAT,
                LibraryDbAdapter.KEY_STORELONG
        };

        // Specify the Corresponding layout elements where we want the columns to go
        int[] to = new int[] {     R.id.sbook,
                R.id.sauthor,
                R.id.sstore,
                R.id.sstoreaddress,
                R.id.slat,
                R.id.slong};

        // Create a simple cursor adapter for the definitions and apply them to the ListView
        SimpleCursorAdapter books = new SimpleCursorAdapter(this,R.layout.bookresult, cursor, from, to);
        mListView.setAdapter(books);

        // Define the on-click listener for the list items
        mListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Get the cursor, positioned to the corresponding row in the result set
                Cursor cursor = (Cursor) mListView.getItemAtPosition(position);

                // Get the state's capital from this row in the database.
                String Book = cursor.getString(cursor.getColumnIndexOrThrow("Book"));
                String Author = cursor.getString(cursor.getColumnIndexOrThrow("Author"));
                String Store = cursor.getString(cursor.getColumnIndexOrThrow("Store"));
                String storeaddress = cursor.getString(cursor.getColumnIndexOrThrow("storeaddress"));
                String lat = cursor.getString(cursor.getColumnIndexOrThrow("lat"));
                String lon = cursor.getString(cursor.getColumnIndexOrThrow("lon"));

                //Check if the Layout already exists
                LinearLayout bookLayout = (LinearLayout)findViewById(R.id.booklayout);
                if(bookLayout == null){
                    //Inflate the Customer Information View
                    LinearLayout leftLayout = (LinearLayout)findViewById(R.id.rightLayout);
                    View bookInfo = getLayoutInflater().inflate(R.layout.bookinfo, leftLayout, false);
                    leftLayout.addView(bookInfo);
                }

                //Get References to the TextViews
                bookName = (TextView) findViewById(R.id.book);
                bookAuthor = (TextView) findViewById(R.id.author);
                storeName = (TextView) findViewById(R.id.store);
                storeAddress = (TextView) findViewById(R.id.storeaddress);
                storeLat = (TextView) findViewById(R.id.latitude);
                storeLong = (TextView) findViewById(R.id.longitude);

                // Update the parent class's TextView
                bookName.setText(Book);
                bookAuthor.setText(Author);
                storeName.setText(Store);
                storeAddress.setText(storeaddress);
                storeLat.setText(lat);
                storeLong.setText(lon);

                searchView.setQuery("",true);
            }
        });
    }
}

private void displayDate() {

    final Calendar c = Calendar.getInstance();

    inspectionDate.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(c.get(Calendar.MONTH) + 1).append("/")
                    .append(c.get(Calendar.DAY_OF_MONTH)).append("/")
                    .append(c.get(Calendar.YEAR)).append(" "));
}
}

现在让我们说我想要另一个功能,但这一次要检查用户是否空闲,但是使用相同的标志。我试图否定返回的值,但我想知道是否有更好的方法来执行此操作。

isUserPremium() {
  return this.flags & Flags.PREMIUM; // returns true
}

1 个答案:

答案 0 :(得分:2)

无法检查是否未使用单个运算符设置标志。我建议稍后在代码中使用!isUserPremium()而不是isUserFree() - 不要创建反转从另一个函数返回的值的函数。但是,请确保您不依赖于此以确保安全性。浏览器中执行的所有操作都可以轻松操作。