Home | History | Annotate | Download | only in downloadablefonts
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.downloadablefonts;
     18 
     19 import android.support.annotation.NonNull;
     20 import android.support.annotation.Nullable;
     21 
     22 /**
     23  * Builder class for constructing a query for downloading a font.
     24  */
     25 class QueryBuilder {
     26 
     27     @NonNull
     28     private String mFamilyName;
     29 
     30     @Nullable
     31     private Float mWidth = null;
     32 
     33     @Nullable
     34     private Integer mWeight = null;
     35 
     36     @Nullable
     37     private Float mItalic = null;
     38 
     39     @Nullable
     40     private Boolean mBesteffort = null;
     41 
     42     QueryBuilder(@NonNull String familyName) {
     43         mFamilyName = familyName;
     44     }
     45 
     46     QueryBuilder withFamilyName(@NonNull String familyName) {
     47         mFamilyName = familyName;
     48         return this;
     49     }
     50 
     51     QueryBuilder withWidth(float width) {
     52         if (width <= Constants.WIDTH_MIN) {
     53             throw new IllegalArgumentException("Width must be more than 0");
     54         }
     55         mWidth = width;
     56         return this;
     57     }
     58 
     59     QueryBuilder withWeight(int weight) {
     60         if (weight <= Constants.WEIGHT_MIN || weight >= Constants.WEIGHT_MAX) {
     61             throw new IllegalArgumentException(
     62                     "Weight must be between 0 and 1000 (exclusive)");
     63         }
     64         mWeight = weight;
     65         return this;
     66     }
     67 
     68     QueryBuilder withItalic(float italic) {
     69         if (italic < Constants.ITALIC_MIN || italic > Constants.ITALIC_MAX) {
     70             throw new IllegalArgumentException("Italic must be between 0 and 1 (inclusive)");
     71         }
     72         mItalic = italic;
     73         return this;
     74     }
     75 
     76     QueryBuilder withBestEffort(boolean bestEffort) {
     77         mBesteffort = bestEffort;
     78         return this;
     79     }
     80 
     81     String build() {
     82         if (mWeight == null && mWidth == null && mItalic == null && mBesteffort == null) {
     83             return mFamilyName;
     84         }
     85         StringBuilder builder = new StringBuilder();
     86         builder.append("name=").append(mFamilyName);
     87         if (mWeight != null) {
     88            builder.append("&weight=").append(mWeight);
     89         }
     90         if (mWidth != null) {
     91             builder.append("&width=").append(mWidth);
     92         }
     93         if (mItalic != null) {
     94             builder.append("&italic=").append(mItalic);
     95         }
     96         if (mBesteffort != null) {
     97             builder.append("&besteffort=").append(mBesteffort);
     98         }
     99         return builder.toString();
    100     }
    101 }
    102